convert string to int32

C

cj2

BalResp is a string filled by a web service.
mAvailBal is an int32

If IsNumeric(BalResp) Then
mAvailBal = BalResp
End If

Is it ok to let VB do the conversion or is there a preferred way?
 
H

Herfried K. Wagner [MVP]

cj2 said:
BalResp is a string filled by a web service.
mAvailBal is an int32

If IsNumeric(BalResp) Then
mAvailBal = BalResp
End If

Is it ok to let VB do the conversion or is there a preferred way?

I suggest to use 'Integer.TryParse'/'Integer.Parse'.
 
J

Jack Jackson

BalResp is a string filled by a web service.
mAvailBal is an int32

If IsNumeric(BalResp) Then
mAvailBal = BalResp
End If

Is it ok to let VB do the conversion or is there a preferred way?

My advice is no. In fact, for that code to compile you must be using
OPTION STRICT OFF. I strongly urge you to use OPTION STRICT ON. It
will make you explicitly do the conversions, but it will also catch
mistakes you made.

For the conversion, I would either use Convert.ToInt32 inside a
Try/Catch, or Integer.TryParse. You probably don't want your code to
throw an exception if you get passed something you don't expect.
 
J

\Ji Zhou [MSFT]\

Hello Chris,

The Int32.MaxValue is 0x7fffffff, 2147483647 in decimal. And its MinValue
is -2147483648. So if you have a string which represents a integer out of
this range, like 21474836471, the application will fail with an exception
"Arithmetic operation resulted in an overflow" and quit unexpectedly.

Two ways to avoid this runtime error,

1.As Jack mentioned, the good convention is turning on Option Strict
switch. So the strong type conversion check will report us the possible
conversion fault in the compiler time. The document of Option Strict is
available here
http://msdn.microsoft.com/en-us/library/zcd4xwzs(VS.80).aspx.

2.We have to write our algorithm to make sure the string passed in is valid
to be converted to Int32. That is to say, it should be in the range
(-2147483648, 2147483647). But that may need a lot of codes for many
considerations. Thus, the recommendation is the first way.

Please let me know if you have any questions or concerns on this. Have a
good day Chris!


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

cj2

Oh God not with the option strict stuff again. I don't make mistakes
for it to catch. ;) Anyway thanks for the other suggestions.
 

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

Top