2

In my new CF winform I suddenly cannot code int.TryParse(), with VS showing the message 'int' does not contain a definition for 'TryParse', but in another form this is no problem. So strange!

I tracked this down, with Go To Definition, that the old form uses int from C:\DOCUME~1\XPMUser\LOCALS~1\Temp\3464$CommonLanguageRuntimeLibrary$v2.0.50727\System.Int32.cs which of course has TryParse(), while my new code uses int from C:\DOCUME~1\XPMUser\LOCALS~1\Temp\3464$mscorlib.dll$v2.0.50727\System.Int32.cs, which has no TryParse().

In the list of references of the project, there is a mscorlib, and by double-clicking I arrive in the Object Browser at mscorlib [Compact Framework], while the object browser also shows an entry mscorlib, i.e. without the CF remark.

This did not help me much, hopefully someone can help me get TryParse() back.

Here is sample code with the error:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace SIClient2.WebServices
{
    class Test
    {
        public Test()
        {
            int i;
            int.TryParse("123", out i); // <== 'int' does not contain a definition for 'TryParse'
        }
    }
}

The code without the error is part of a winform. Does that make a diff?

Roland
  • 4,619
  • 7
  • 49
  • 81
  • 1
    Did you install all .NET frameworks? If you're on Windows 7+, then they should all be there, so check. Are you overriding the method? We need to see the code itself to see what went wrong. – Ilan Mar 21 '14 at 16:10
  • 1
    .NET CF only partially supports this method. It certainly doesn't support a TryParse() overload with no arguments. Never forget to post a snippet. – Hans Passant Mar 21 '14 at 16:21
  • I inherited this project, and in another form of this same project int does have a .TryParse(). So how could something be installed incorrectly? This really baffles me. BTW this is XP mode under Win7. I will post some actual code. – Roland Mar 21 '14 at 16:22
  • 1
    TryParse has never existed in any version of the CF for any primitive. If you had it, it was from some other library and you need to find that library (or just re-code it yourself). – ctacke Mar 21 '14 at 16:24
  • I'd recommend Ben's answer here (not mine): http://stackoverflow.com/questions/6563968/c-sharp-integer-validation-compactframework/6564025#6564025 – ctacke Mar 21 '14 at 16:27
  • I will definitely use Ben's alternative if needed, but I find it puzzling why I should need it while it worked in other places of the same project. – Roland Mar 21 '14 at 16:31
  • I'm telling you that if it ever worked it's because you pulled in a library that had it implemented. `TryParse` has never existed in any version of the CF, so it could have never worked just using the BCL. – ctacke Mar 21 '14 at 16:33
  • Well, I posted that the other `int` is found in another library. My problem is how to get one more `int` from that lib instead from the one marked CF. – Roland Mar 21 '14 at 16:36
  • Somehow you have it referencing a desktop BCL, which allows it to compile. At runtime under the CF it will fail. (in fact deployment should be a problem from the debugger as I would expect Studio to try to push the entire desktop framework to the device). – ctacke Mar 21 '14 at 16:39
  • Ok you convinced me of using another implementation of TryParse, such as Ben's above. Thanks for your help you all. Have a nice weekend. – Roland Mar 21 '14 at 16:44
  • This turned out to be one of the things you should not do on a Friday afternoon. The solution has several projects: a client on a mobile device, hence CF. A server for web services. And a test project that calls the web services directly for easier debugging, with desktop winforms, i.e., without CF, and with int.TryParse() ... :-) We are also working on upgrading to more modern mobile devices which will hopefully avoid CF. – Roland Mar 21 '14 at 17:11
  • `Int32.TryParse("123", out i)` should work, but not the primitive int – mpag Nov 01 '17 at 19:05

1 Answers1

1

This turned out to be one of the things you should not do on a Friday afternoon. The solution has several projects: a client on a mobile device, hence CF. A server for web services. And a test project that calls the web services directly for easier debugging, with desktop winforms, i.e., without CF, and with int.TryParse() ... :-) We are also working on upgrading to more modern mobile devices which will hopefully avoid CF.

The bottom line is that VS is not that bad, but not very easy either. Most errors are valid pointers to real problems. On the other hand regrettable that CF is so limited, while today's mobile devices are not that petite anymore.

Roland
  • 4,619
  • 7
  • 49
  • 81