Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer.TryParse - a better way?

I find myself often needing to use Integer.TryParse to test if a value is an integer. However, when you use TryParse, you have to pass a reference variable to the function, so I find myself always needing to create a blank integer to pass in. Usually it looks something like:

Dim tempInt as Integer If Integer.TryParse(myInt, tempInt) Then 

I find this to be quite cumbersome considering that all I want is a simple True / False response. Is there a better way to approach this? Why isn't there an overloaded function where I can just pass the value I want to test and get a true / false response?

like image 432
Ryan Smith Avatar asked Jan 07 '09 02:01

Ryan Smith


People also ask

Why should one use TryParse instead of parse?

Parse() method throws an exception if it cannot parse the value, whereas TryParse() method returns a bool indicating whether it succeeded. However, TryParse does not return the value, it returns a status code to indicate whether the parse succeeded and does not throw exception.

What does int TryParse do?

TryParse(String, Int32) Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.

What does TryParse return if successful?

TryParse method converts a string value to a corresponding 32-bit signed integer value data type. It returns a Boolean value True , if conversion successful and False , if conversion failed.

What happens if TryParse fails?

When this method returns, contains the 32-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. But what happens if the passed string itself is a string representation of '0'. So the TryParse will return zero.


2 Answers

No need to declare the integer.

If Integer.TryParse(intToCheck, 0) Then 

or

If Integer.TryParse(intToCheck, Nothing) Then 

If you have .Net 3.5 ability you can create an extension method for strings.

Public Module MyExtensions      <System.Runtime.CompilerServices.Extension()> _     Public Function IsInteger(ByVal value As String) As Boolean         If String.IsNullOrEmpty(value) Then             Return False         Else             Return Integer.TryParse(value, Nothing)         End If     End Function  End Module 

And then call like:

If value.IsInteger() Then 

Sorry, getting carried away I know, but also you can add this to the MyExtensions class above in .Net 3.5 and not worry unless you need validations.

<System.Runtime.CompilerServices.Extension()> _ Public Function ToInteger(ByVal value As String) As Integer     If value.IsInteger() Then         Return Integer.Parse(value)     Else         Return 0     End If End Function 

Then simply use

value.ToInteger() 

This will return 0 if it isn't a valid Integer.

like image 147
Tom Anderson Avatar answered Sep 23 '22 12:09

Tom Anderson


Since you are using VB.net you can use the IsNumeric Function

If IsNumeric(myInt) Then     'Do Suff here End If 
like image 33
TonyB Avatar answered Sep 19 '22 12:09

TonyB