How to determine if an object is numeric?

E

Ethan Strauss

Hi,
In C#.net 1.1, is there a simple way to determine if an arbitrary object
can be cast as a number? How about if it can be cast as a int? The only ways
I can find are to either try it and see if an error is thrown, or to check
every character in the object cast as a string to see if it fits. Either way
is painful.
I would guess that there is some sort of CheckNumber(),CheckInt()
method, but I can't find it. Can anyone help?
Thanks,
Ethan
 
N

Nicholas Paldino [.NET/C# MVP]

Ethan,

Assuming you are working with object, you are going to have issues
trying to do an unboxing situation. Take the following example:

byte b = 10;

// There is an implicit cast, and it is valid.
int i = b;

object o = b;

// This will throw an exception.
int i = (int) o;

The problem here is that when unboxing, you have to unbox from the
object to the type that was boxed, in this case, a byte. However, I assume
you want some sort of code that doesn't know a byte is stuffed into an
object (and even if you did reflect it, you aren't going to have hard coded
casts for each type). Because of this, it prevents you from using a
try/catch block where you cast/unbox to an int and then return.

In this case, I would use the static ToInt32 method on the Convert class
to try and perform a conversion. Of course, you would wrap this in a
try/catch block, to see if you tried to convert a type that couldn't be
converted.

Hope this helps.
 
R

Rad [Visual C# MVP]

Ethan,

Assuming you are working with object, you are going to have issues
trying to do an unboxing situation. Take the following example:

byte b = 10;

// There is an implicit cast, and it is valid.
int i = b;

object o = b;

// This will throw an exception.
int i = (int) o;

The problem here is that when unboxing, you have to unbox from the
object to the type that was boxed, in this case, a byte. However, I assume
you want some sort of code that doesn't know a byte is stuffed into an
object (and even if you did reflect it, you aren't going to have hard coded
casts for each type). Because of this, it prevents you from using a
try/catch block where you cast/unbox to an int and then return.

In this case, I would use the static ToInt32 method on the Convert class
to try and perform a conversion. Of course, you would wrap this in a
try/catch block, to see if you tried to convert a type that couldn't be
converted.

Hope this helps.

Short of trying to cast the objects and catching exceptions, I don't think
you have much of a choice. If the objects correctly overload ToString() you
could craft a regular expression to evaluate the returned string, but I'm
not sure as to the performane (or wisdom!) of such an approach. What manner
of objects are you dealing with?
 
E

Ethan Strauss

Thanks for the answers,
In this case I am actually dealing with an arraylist which could contain
either string objects or int objects, but I have had this same sort of
situation come up before in other cases, so I asked the generic question. I
have dealt with it with a Regex in the past, but it is quite a pain.
In my particular situation today, I found a way around it so I know what
is going into the array list. In the future I may just try casting and catch
the exception.

Thanks again,
Ethan


I think I may just try the cast and see if I get an
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top