convert string to int

  • Thread starter Thread starter William Stacey
  • Start date Start date
W

William Stacey

Is there a CanConvert like method that returns a bool instead of an
exception. The problem with wrapping in try is the wait time for the
exception.
 
Thanks. That was point of checking for isnumeric first, so you don't have
to hit the exception.

--
William Stacey, MVP

Daniel Bass said:
You're slightly missing the point then if the only check you have is an
exception.
....
 
Thats good info. I would think that after you got that far, converting to a
number yourself could be faster then calling int.parse() or the like.
 
I built this for fun for checking unsigned integers and getting bool return
plus the value if method could convert the string. Probably much faster
ways, but this works so far:

public static bool IsUInt(string str, out uint val)
{
val = 0;
if ( str == null || str.Trim() == "" )
return false;

char [] cArray = str.ToCharArray();
if ( cArray.Length > 10 || cArray.Length == 0 )
return false;

long numPlace = 1;
long num = 0;
byte b = 0;
for (int i = cArray.Length - 1; i >= 0; i--)
{
if (cArray > 57 || cArray < 48)
return false;
b = (byte)(cArray - 48);
num = num + ( b * numPlace );
numPlace = numPlace * 10;
}

if ( num > int.MaxValue )
return false;

val = (uint)num;
return true;
}
 
I fixed a small issue and went ahead and did string and char[] versions for
IsUInt and IsInt :

public static bool IsUInt(string str, out uint val)
{
val = 0;
if ( str == null )
return false;
char [] cArray = str.ToCharArray();
return Bits.IsUInt(cArray, out val);
}

public static bool IsUInt(char[] cArray, out uint val)
{
val = 0;
if ( cArray == null || cArray.Length == 0 )
return false;

if ( cArray.Length > 10 )
return false;

long numPlace = 1;
long num = 0;
byte b = 0;
for (int i = cArray.Length - 1; i >= 0; i--)
{
if (cArray > 57 || cArray < 48)
return false;
b = (byte)(cArray - 48);
num = num + ( b * numPlace );
numPlace = numPlace * 10;
}

if ( num > uint.MaxValue )
return false;

val = (uint)num;
return true;
}

public static bool IsInt(string str, out int val)
{
val = 0;
if ( str == null )
return false;
char [] cArray = str.ToCharArray();
return Bits.IsInt(cArray, out val);
}

public static bool IsInt(char[] cArray, out int val)
{
val = 0;
if ( cArray == null || cArray.Length == 0 )
return false;

if ( cArray.Length > 11 ) //10 max numerics plus one minus sign.
return false;

long numPlace = 1;
long num = 0;
byte b = 0;
int signed = 1;

if ( cArray[0] == '-' )
{
signed = -1;
cArray[0] = '0';
if ( cArray.Length == 1 )
return false;
}

for (int i = cArray.Length - 1; i >= 0; i--)
{
if (cArray > 57 || cArray < 48)
return false;
b = (byte)(cArray - 48);
num = num + ( b * numPlace );
numPlace = numPlace * 10;
}

num = num * signed;

if ( num > int.MaxValue || num < int.MinValue )
return false;

val = (int)num;
return true;
}
 
int myInt = System.Convert.ToInt32 ( name of your item )
Dan.

Please, how can I convert selected item from dropdown list
to integer?
Thank you
 
Are you thinking about

int selected = comboBox1.SelectedIndex;

Or just how to convert a string to integer?

int number = Convert.ToInt32(text);
int number = Int32.Parse(text);
 
Hi,

If you are sure that the values of the dropdown are numbers you can do
like:
int val = Convert.ToInt32( DropDownControl. SelectedValue );

If you are not then you can wrap the above line in a try/catch block.


Cheers,
 
Hi William,

AFAIK there is not such a method in the framework, what you can do is
include the VB.NET DLL which define a IsNumeric function.


Cheers,
 
Hi William,

You might also use Regex as it should be fairly simple to check for the
integers.
 
Hi Ignacio,
one problem with VB IsNumeric: does not explicitly check for integers.
that means IsNumeric() does also return true, if your string is a double(or
float). that's not always what yout want.

Reinhold
(Sorry for bad english)
 
You're slightly missing the point then if the only check you have is an
exception.

Exceptions are meant to catch errors that are unforseen, and while .Net (in
particular) c# and VB.Net seem to use more exceptions for returning errors
(rather than a bool or hresult), good code (i.e. code that's past the beta
stage of testing and in the RC) should never hit an exception.

wrapping code in try/catch is essential for reporting to you when things
don't go as you'd expect... if you leave the door open (ie an alpha numeric
text box) for user errors then there should be tighter restrictions made on
what the user can do to prevent this from happening.

If you hit a situation where you're trying to access an object for a
conversion to int and it can't be a converted to an int, then your code
needs reviewing, and perhaps tighter controls implemented, but the exception
should still be there to catch anything that goes wrong.

In this case, the initial enquiry has little weight since an exception is
more of a debugging facility when the unexpected errors occur, rather than
handling everything that goes wrong that could go wrong, because it's not
been catered for.

Hope that helps.
Dan.

Is there a CanConvert like method that returns a bool instead of an
exception. The problem with wrapping in try is the wait time for the
exception.
 
the regex is probably one of the more efficient ways of confirming the
number...

Not really. Going through the characters in the string and checking for
digits is *far* more efficient.

Search in groups.google.com for a thread called "Checking if a string
can be converted to Int32" in microsoft.public.dotnet.framework. The
results from a bit of benchmarking were (in times for checking 9
strings 100,000 times each):

Just using an exception: 1m 16s
A hand-crafted checking routine: 0.7s
Double.TryParse (for first pass removals): 41s
Regular expression: 43s
The VB "IsNumeric" method: 1m 7s

Regular expressions are incredibly powerful things, but for simple
tests like this, hand-crafted code is almost always going to be more
efficient.
 
William said:
Is there a CanConvert like method that returns a bool instead of an
exception. The problem with wrapping in try is the wait time for the
exception.

I like to use double.TryParse. It returns false if you pass an invalid
string. It also allows for any numeric formatting specified in the
user's Control Panel setting and is culturally aware. Convert and
int.Parse are entirely too finicky when compared to TryParse.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
William Stacey said:
Thats good info. I would think that after you got that far, converting to a
number yourself could be faster then calling int.parse() or the like.

Hmm... possibly, but it would be significantly harder, I suspect. There
are cases that my code still won't catch - I still rely on int.Parse()
throwing an exception in the rare cases that I miss.
 
Back
Top