How to Convert a String to INT in Vb.net

Save

Converting strings into integers safely helps make your VB.NET application more stable. A user entering "xyz" into a numerical field, for example, can cause your program to crash if your code fails to catch that error. One way to prevent problems is to examine a string before attempting to convert it. You could do that by adding lengthy exception checking to your code. A faster way to convert a string is to use the "TryParse" method built into the .NET framework. The "TryParse" method validates a string, and converts it into an integer if the string contains integer data.

  • Open one of your VB.NET projects using Microsoft Visual Studio. Locate the Page_Load method in the project's startup form.

  • Paste the following code at the top of that method:

    Dim stringValue1 As String = "100"

    Dim stringValue2 As String = "100.1"

    Dim stringValue3 As String = "ABCD"

    MessageBox.Show(MakeInt(stringValue1))

    MessageBox.Show(MakeInt(stringValue2))

    MessageBox.Show(MakeInt(stringValue3))

    This creates three string variables, and assigns them values. The first variable holds "100." That is a valid integer value. The second variable's value is "100.1." That is a valid number, but it is not a valid integer. The final variable, "stringValue3," has an invalid value. That value is "ABCD." The last three statements pass the three variables to a function named "MakeInt." This function attempts to convert each string to an integer and returns the result of that attempt to the Page_Load method. The MessageBox.Show commands display the values returned from the function calls.

  • Paste the following function below the Page_Load method:

    Function MakeInt(ByVal val As String) As Integer

    Dim intVal As Integer

    If Not Integer.TryParse(val, intVal) Then

     intVal = -1

    End If

    Return intVal

    End Function

    This function uses the "TryParse" method to convert the string value it receives into an integer. If it succeeds, it stores the result in the "intVal" variable. If the conversion attempt fails, the function sets the value of "intVal" variable to "-1."

  • Press your "F5" key. Visual Studio runs the project. The first "MessageBox.Show" command displays "100" because the function was able to convert the string containing "100" into a valid integer. The next two "MessageBox.Show" commands display "-1." Those values, "100.1" and "ABCD," are not integers. The function returns "-1" because it could not convert those values into integers.

Tips & Warnings

  • Be sure to examine the values returned from the "MakeInt" function. When it returns a value of -1, your code knows not perform calculations using that value.

References

  • Photo Credit John Foxx/Stockbyte/Getty Images
Promoted By Zergnet

Comments

Resources

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!