IsNumeric: Convert.ToInt32 vs. Convert.ToInt64

S

sck10

Hello,

I am trying to test for IsNumeric using C# with the following:

public bool IsNumeric(object TestValue)
{
try
{
int i = Convert.ToInt32(TestValue.ToString());
return true;
}
catch (FormatException)
{
return false;
}
}


If I use ToInt32, I get the error: "System.OverflowException: Value was
either too large or too small for an Int32." For a value of 9999999999

If I try: int i = Convert.ToInt64(TestValue.ToString());

Cannot implicitly convert type 'long' to 'int'. An explicit conversion
exists (are you missing a cast?)

Any assistance on why Convert.ToInt64 is failing would be appreciated.
Thanks, sck10
 
M

Mark Rae

If I try: int i = Convert.ToInt64(TestValue.ToString());

Cannot implicitly convert type 'long' to 'int'. An explicit conversion
exists (are you missing a cast?)

Any assistance on why Convert.ToInt64 is failing would be appreciated.

Er, because you're trying to plug an Int64 into an Int32... :)

Would this work...?
byte i = Convert.ToInt64(TestValue.ToString());

Why not...?

For the same reason, neither will:
int i = Convert.ToInt64(TestValue.ToString());

This will, though...
Int64 i = Convert.ToInt64(TestValue.ToString());
 
G

Guest

Why are you still trying the exception catching approach? This question was
answered the last time you posted a few hours previously.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
S

sck10

Thanks David,

I am using the approach you presented earlier (from your website), but I was
interested in learning why .ToInt64 was not working.

Thanks again...
 
D

daniel

I recommend to use a regular expression to IsNumeric Method, is more
eficcient.

regards
daniel
 

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

Similar Threads

IsNumeric 12
Casting RadioButtonList 4
How to try a range of hex values in C# code ? 0
Please, help me. 1
ImageHandler question 2
Replies to Upcasting vs downcasting. 9
Memory : Stack vs Heap 9
C# help needed 4

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top