473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

IsNumeric: Convert.ToInt32 vs. Convert.ToInt64

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
Sep 2 '06 #1
4 6827
"sck10" <sc***@online.nospamwrote in message
news:Oy****************@TK2MSFTNGP02.phx.gbl...
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());
Sep 2 '06 #2
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#
"sck10" wrote:
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
Sep 3 '06 #3
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...
"David Anton" <Da********@discussions.microsoft.comwrote in message
news:E9**********************************@microsof t.com...
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#
"sck10" wrote:
>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

Sep 3 '06 #4
I recommend to use a regular expression to IsNumeric Method, is more
eficcient.

regards
daniel

"sck10" <sc***@online.nospamwrote in message
news:Oy****************@TK2MSFTNGP02.phx.gbl...
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

Sep 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Daniel Billingsley | last post by:
Is there any significant difference between int variable1 = (int) variable2; and int variable1 = Convert.ToInt32(variable2); It seems like the extra typing and granularity of .ToInt32 vs....
10
by: PeteZ | last post by:
or do I have to iterate the string and use Char.IsNumber ?
10
by: crowl | last post by:
I have a pocket pc project in c#. From a textbox i have to verify if the input is numeric. I have found in the msdn a sample like this: textbox1.numeric = true / false. But this do not work in...
3
by: aaa | last post by:
I have a third party DLL that downloads everything from a device to a particular device into the following Hex format: 080 : 13 70 05 00 (Timestamp) 081 : 35 E1 91 F9 9F BF (Mode 1 Supported...
3
by: Chua Wen Ching | last post by:
I have a problem. But on .NET 1.1 My Scenario: Actually I will have a string of hexadecimals read from a xml file. Then from the hexadecimals, i will add 1 value whenever i made any...
5
by: Steve Kershaw | last post by:
Hello, I have a need to see if a string is numeric or alphabetic. I understand that Visual Basic has a method called "IsNumeric(string)" but C# dosen't appear to have one. Any ideas? Thanks for...
17
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
12
by: sck10 | last post by:
Hello, I am trying to determine if a value is NOT numeric in C#. How do you test for "Not IsNumeric"? protected void fvFunding_ItemInserting_Validate(object sender, FormViewInsertEventArgs...
10
by: =?Utf-8?B?Um95?= | last post by:
What is the way to have best performance to copy a byte to a value such as long? I use BitConverter.ToInt64(binary, offset) But the performace is not good enough. I need to have the best...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.