handling int value...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

greetings,

i'm converting my value to int value using int32.parse()
the problem is that, if its an empty value it returns me an error. what can i do to handle this error besides using a if condition to handle it.
 
what can i do to handle this error besides using a if condition to
handle it.

Use a Try, Catch statement.

Try
'Convert to into
Catch ex as InvalidCastException (or Exception)
'int = 0
End Try
 
hi asha ,

maybe a function like this may help you : (this function will return zero if
it can not convert string to number

public static int ConvertToInt(object stringNumber)

{

int retval=0;

try

{

retval=Convert.ToInt32(stringNumber);

}

catch

{

}

return(retval);

}



Asha said:
greetings,

i'm converting my value to int value using int32.parse()
the problem is that, if its an empty value it returns me an error. what
can i do to handle this error besides using a if condition to handle it.
 
using try/catch to handle expected values is a really bad practice. the
performance cost of thowing an error is very high. the code should be:

public static int ConvertToInt(object stringNumber)
{
if (stringNumber.Length == 0)
return 0;
return Convert.ToInt32(stringNumber);
}

if you want to eat bad data and return 0 for it, then:

public static int ConvertToInt(object stringNumber)
{
if (stringNumber.Length == 0)
return 0;
try
{
return Convert.ToInt32(stringNumber);
}
catch
{
return 0;
}
}
 
You're right about the try/catch being a big performance hit for converting
strings to int but the hit mostly comes in the catch. If all works well, it
is pretty fast.

So then the issue, as I see it, is that if you're going to respond to the
user and wait for user action or do nothing because the data is bad then
this hit isn't as significant and you could choose to live with it. If
you're going to handle it yourself in code, such as assigning 0 as a
default, and then continue processing, the overhead of catching the error is
significant and easily noticible.

Your ConvertToInt method fails because if stringNumber is not really
numeric, it will still throw an exception. The second example is still just
a try/catch.

In reality, I think many developers tend to use the try/catch even though it
really is a bad idea in most cases. I don't see any C# code around that has
a method for evaluating a string to see if it can be converted to a numeric
value.

Visual Basic.NET has the IsNumeric method that fills the bill and if Ersin
is developing in VB.NET, that is the solution.

In C#, only the Double structure has some handling for bad numbers in the
form of Double.TryParse() which will return false if the conversion fails.
Look at this link from Microsoft to see how to emulate IsNumeric in C# with
the TryParse method:

http://support.microsoft.com/default.aspx?scid=kb;en-us;329488

Alternatively, a google search on regex AND isnumeric will return a lot of
different ways to validate the string with a regular expression but then you
still have to do the conversion.

Hope this helps,

Dale Preston
MCAD, MCSE, MCDBA
 
I used to have this problem, but then I created a method myself that
converts a string to an integer. You can provide a default value that
is returned if the string couldn't be converted. No exceptions are
thrown. Our measurements (at work) showed us that it is more than 3
times faster than Int32.Parse (which is called by Convert.ToInt32). It
even handles leading and trailing whitespace (the routine considers
all characters < ' ' (32) to be whitespace).

The code below has been tested very well and has been running in
production software on both client and server machines for about a
year now.

public static int ConvertToInt(string str, int defaultValue)
{
if (str == null || str.Length == 0) return defaultValue;
int index = 0;
int length = str.Length;
while (index < length && str[index] <= ' ') index++;
bool negative;
if (str[index] == '-')
{
negative = true; index++;
while (index < length && str[index] <= ' ') index++;
}
else negative = false;
int value = 0;
while (index < length)
{
char ch = str[index];
if (ch <= ' ') break;
else if (ch > '9' || ch < '0') return defaultValue;
else value = value * 10 + ch - '0';
index++;
}
if (negative) return -value; else return value;
}
 
Back
Top