How to Convert a String to INT in Vb.net

Written by kevin lee
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Convert a String to INT in Vb.net
Computers prefer using numbers when processing numerical data. (John Foxx/Stockbyte/Getty Images)

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.

Skill level:
Moderately Easy

Other People Are Reading

Instructions

  1. 1

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

  2. 2

    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.

  3. 3

    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."

  4. 4

    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 and 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.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.