Test Data Type

  • Thread starter Thread starter Guadala Harry
  • Start date Start date
G

Guadala Harry

What are the options for testing to see if a String variable contains a
value that can be converted to an integer (vs containing a value that cannot
be converted to an integer)?

e.g., "784" can be converted to an integer
whereas "nope" cannot be converted to an integer
nor can "" (a zero length string) be converted to an integer
nor can null (the 'null' value) be converted to an integer

Please note that I do not want rely on error handling for this - if the
input value cannot be converted, I need to simply skip the particular input
and proceed to processing others. AFAIK, if I rely on error handling, then
processing would have to stop - or I'd have to get into some ugly recursive
call situation - or I'd have to nest a bunch of try... catch blocks. So,
without doing this test via error handling, what are my options?

Thanks!
 
Guadala,

In .NET 1.1, the options are a little limited. You could try the static
TryParse method on the Double class, and if it returns true, then there is a
number. At that point, you could convert to a double, and then convert that
into an integer.

You could also call the static IsNumeric method on the Interaction class
in the Microsoft.VisualBasic namespace.

In .NET 2.0, the primitive types all expose a static TryParse method, so
you could call the Int32 type's implementation and if it is successful, use
the number returned from that.

Hope this helps.
 
Back
Top