Converting input strings to integers
Is there a standard .NET way to convert input strings to their closest integer
equivalent? In VB.NET I basically do a "CInt" and it takes care of most of
the problem (except for null values).
My first (pathetic non-working) attempt is the following - it currently fails
on non-numeric input or non-integer numbers. Am I totally out to lunch? Or
will I need to convert to a char array and look at each character, etc? I'm
willing to methodically go through that process, so I don't expect anyone
to write that code for me, but I have a hunch I'm going down the wrong track
on this. And of course I've named it "CInt" as any VB diehard would have
done :)
public static int CInt(object oValue)
{
if (oValue == null || System.Convert.IsDBNull(oValue))
{
//default null to 0:
return 0;
}
string sString = oValue.ToString().Trim();
if (sString == "")
{
//default empty string to 0:
return 0;
}
return int.Parse(sString, NumberStyles.Any);
}
[1070 byte] By [
Dave] at [2007-11-9 18:49:52]

# 1 Re: Converting input strings to integers
Look at the various integer types' Parse method. Or see the Convert.ToInt32
method (also see Convert to convert to other integer types). Or feel free to
import the VB namespace and use CInt (and other VB commands) directly.
"Dave" <dave_doknjas@yahoo.ca> wrote in message
news:3f319a37$1@tnews.web.dev-archive.com...
>
> Is there a standard .NET way to convert input strings to their closest
integer
> equivalent? In VB.NET I basically do a "CInt" and it takes care of most of
> the problem (except for null values).
>
> My first (pathetic non-working) attempt is the following - it currently
fails
> on non-numeric input or non-integer numbers. Am I totally out to lunch? Or
> will I need to convert to a char array and look at each character, etc?
I'm
> willing to methodically go through that process, so I don't expect anyone
> to write that code for me, but I have a hunch I'm going down the wrong
track
> on this. And of course I've named it "CInt" as any VB diehard would have
> done :)
>
> public static int CInt(object oValue)
> {
> if (oValue == null || System.Convert.IsDBNull(oValue))
> {
> //default null to 0:
> return 0;
> }
>
> string sString = oValue.ToString().Trim();
> if (sString == "")
> {
> //default empty string to 0:
> return 0;
> }
>
> return int.Parse(sString, NumberStyles.Any);
> }
>
# 2 Re: Converting input strings to integers
"Russell Jones" <arj1@northstate.net> wrote:
>Look at the various integer types' Parse method. Or see the Convert.ToInt32
>method (also see Convert to convert to other integer types). Or feel free
to
>import the VB namespace and use CInt (and other VB commands) directly.
>
>"Dave" <dave_doknjas@yahoo.ca> wrote in message
>news:3f319a37$1@tnews.web.dev-archive.com...
>>
>> Is there a standard .NET way to convert input strings to their closest
>integer
>> equivalent? In VB.NET I basically do a "CInt" and it takes care of most
of
>> the problem (except for null values).
>>
>> My first (pathetic non-working) attempt is the following - it currently
>fails
>> on non-numeric input or non-integer numbers. Am I totally out to lunch?
Or
>> will I need to convert to a char array and look at each character, etc?
>I'm
>> willing to methodically go through that process, so I don't expect anyone
>> to write that code for me, but I have a hunch I'm going down the wrong
>track
>> on this. And of course I've named it "CInt" as any VB diehard would have
>> done :)
>>
>> public static int CInt(object oValue)
>> {
>> if (oValue == null || System.Convert.IsDBNull(oValue))
>> {
>> //default null to 0:
>> return 0;
>> }
>>
>> string sString = oValue.ToString().Trim();
>> if (sString == "")
>> {
>> //default empty string to 0:
>> return 0;
>> }
>>
>> return int.Parse(sString, NumberStyles.Any);
>> }
>>
>
I'm currently using the int type's Parse method from within my routine, but
the problem is (and also with the Convert namespace) is that I'll have to
resort to catching exceptions in this process and I'd rather not do that
as part of normal logic.
Import the VB namespace? *** GASP ***
Ok, maybe, if I have to.
Dave at 2007-11-11 21:59:31 >

# 3 Re: Converting input strings to integers
Hello Dave,
> Is there a standard .NET way to convert input strings to their closest
integer
> equivalent? In VB.NET I basically do a "CInt" and it takes care of
most of
> the problem (except for null values).
Try the static Double.TryParse method.
using System.Globalization;
bool success;
int intValue;
double doubleValue;
string x = "1234";
success = Double.TryParse( x, NumberStyles.Integer,
CultureInfo.CurrentCulture,
out doubleValue )
if( success ) intValue = (int)doubleValue;
If it can't perform the conversion it will return false, which is
better than throwing an exception.
Hope this helps,
Len
# 4 Re: Converting input strings to integers
"Len Weaver" <len.weaver@meritsoft.net> wrote:
>Hello Dave,
>
>> Is there a standard .NET way to convert input strings to their closest
>integer
>> equivalent? In VB.NET I basically do a "CInt" and it takes care of
>most of
>> the problem (except for null values).
>
> Try the static Double.TryParse method.
>
>using System.Globalization;
>
>bool success;
>int intValue;
>double doubleValue;
>string x = "1234";
>
>success = Double.TryParse( x, NumberStyles.Integer,
> CultureInfo.CurrentCulture,
> out doubleValue )
>
>if( success ) intValue = (int)doubleValue;
>
> If it can't perform the conversion it will return false, which is
>better than throwing an exception.
>
>Hope this helps,
>Len
>
>
Perfect!! Thanks Len!
I couldn't find anything relevant for the int type, so I didn't even bother
to look at the double type. This is exactly the kind of thing I was looking
for.
Dave at 2007-11-11 22:01:39 >

# 5 Re: Converting input strings to integers
thanx Len, i'm kinda n00b in VC# porting myself from VB6. This is exactly what i was looking 4. :)
# 6 Re: Converting input strings to integers
You could also use the IsNumeric method, but that would also require importing VB.
# 7 Re: Converting input strings to integers
Unfortunately the TryParse is only available for Double. I wish it was available for other conversions...