0

In moving some code over from my test project to the "real" project, which targets Windows CE, some code got embarrassed and turned red in the IDE, namely "TryParse()".

For the lack of ... a horseshoe, the battle was lost; hopefully, the lack of TryParse() will not cause a healthcare.gov-like eCatastrophe; nevertheless is there a better way to rewrite a TryParse() for a TryParseless parser than:

int recCount = 0;
string s = "42";
try {
    recCount = Int32.Parse(s);      
}
catch {
    MessageBox.Show("That was not an int! Consider this a lint-like hint!");
}

?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    If `s` is a string, that will never *succeed*. In fact, it won't even *compile* because the compiler can be 100% sure it will never succeed. – Servy Nov 26 '13 at 17:49
  • True; I changed it, but my question remains the same: is there a less kludgy way of accomplishing this, given the limitations of WindowsCE target? – B. Clay Shannon-B. Crow Raven Nov 26 '13 at 17:54

4 Answers4

3

Considering s is a string value, you can't cast it to int. If int.TryParse is not available then you can create your own method which would return a bool . Something like:

public static class MyIntConversion
{
    public static bool MyTryParse(object parameter, out int value)
    {
        value = 0;
        try
        {
            value = Convert.ToInt32(parameter);
            return true;
        }
        catch
        {
            return false;
        }
    }
}

and then to use it:

int temp;
if (!MyIntConversion.MyTryParse("123", out temp))
{
     MessageBox.Show("That was not an int! Consider this a lint-like hint!");
}

int.TryParse internally uses try-catch to do parsing and is implemented in similar manner.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • 1
    Per this question: http://stackoverflow.com/questions/15294878/how-the-int-tryparse-actually-works, I don't think `int.TryParse` uses `try-catch`. I'd still write it that way if it wasn't available. – Joel Rondeau Nov 26 '13 at 18:02
  • @JoelRondeau, I am actually going through the source, but yes it appears `int.TryParse` doesn't uses exception handling – Habib Nov 26 '13 at 18:04
2
public bool TryParseInt32( this string str, out int result )
{
    result = default(int);

    try
    {
        result = Int32.Parse( str );
    }
    catch
    {
        return false;
    }

    return true;
}

usage:

int result;
string str = "1234";

if( str.TryParseInt32( out result ) )
{
}
Moho
  • 15,457
  • 1
  • 30
  • 31
1

I'm assuming s is a string. If so, your code won't work. The following code should though:

int recCount = 0;
try {
    recCount = Int32.Parse(s);      
}
catch {
    MessageBox.Show("That was not an int! Consider this a lint-like hint!");
}
System Down
  • 6,192
  • 1
  • 30
  • 34
1

You could use a regular expression.

public bool IsNumber(string text)
{
    Regex reg = new Regex("^[0-9]+$");
    bool onlyNumbers = reg.IsMatch(text);
    return onlyNumbers;
}
fabricio
  • 1,385
  • 17
  • 22