How to Use TryParse in Visual Basic

Save

Many of the numbers that you see in computer applications are actually strings. For example, a "Quantity" text box on a Web page may contain a string representation of the number "100." Developers must convert string values into numeric values before computers can use them to perform mathematical calculations. Microsoft's Visual Basic programming language has a built-in class called "TryParse." This class contains methods that allow you to convert string representations of numbers into numeric data type that computers can manipulate.

  • Launch Microsoft Visual Studio. Click "File," and then click "New."

  • Click "New Project" to open the "New Project" window. Click "Visual Basic," and then click "Console Application." Visual Studio creates a new console application and displays the code for "Module1.vb" in the code window. This code contains the "Main" sub that contains these two lines:

    Sub Main()

    End Sub

  • Replace those two lines of code with the following code:

    Sub Main()

    Dim int16String As String = "100"

    Dim doubleString As String = "100"

    Dim byteString As String = "1"

    Dim success As Boolean

    End Sub

    This creates a "Main" sub that contains four variable definitions. The first three lines in the "Main" Sub define an Int32, a Double and a Byte data type. These are common data types that you might use in a Visual Basic program. The fourth line defines a Boolean variable named "success."

  • Add the following code after the line that reads "Dim success as Boolean:"

    success = Int16.TryParse("100", int16Number)

    Console.WriteLine("success converting 100 to Int16 = " & success)

    success = Double.TryParse("10 0", doubleNumber)

    Console.WriteLine("success converting 10 0 to Double = " & success)

    success = Byte.TryParse("1", byteNumber)

    Console.WriteLine("success converting 1 to Byte = " & success)

    Console.ReadLine()

    The first line of code attempts to convert the string "100" to an Int16 data type using the "Int16.TryParse" method. If the conversion succeeds, Visual Basic stores the value in the variable named "int16Number." Visual Basic also stores the status of that conversion attempt in the "success" variable. The second line of code displays the value of "success." That value will be "true" if the conversion works and "false" if the conversion fails. The remaining lines of code perform the same conversion attempt using the "Double.TryParse" method and the "Byte.TryParse" method. The "Console.WriteLine statements write these results to the command window. The final line of code keeps the command window open so that you can view the results.

  • Press F5. Visual Studio runs the application. The TryPasre methods attempt to convert "100," "10 0" and "1" to the data types specified in the TryParse statements.

  • Review the results in the command window. Those results will appear as follows:

    success converting 100 to Int16 = True

    success converting 10 0 to Double = False

    success converting 1 to Byte = True

    The second conversion from "10 0" to Double fails and returns "False" because the string "10 0" contains an embedded space. It is not a valid number.

  • Continue to experiment using different string values. Note how the TryParse method works with those values. For instance, instead of using "10 0" in the Double.TryParse statement, you might try a string value such as "-100.0" instead.

Tips & Warnings

  • Each numeric Visual Basic data type has a TryParse method. These methods include int32.TryParse, Char.TryParse and even DateTime.TryParse. Click Visual Studio's "Help" button and search for "TryParse" for additional details.
  • The TryParse method is also useful for making sure that users enter valid numbers into text boxes. To validate a user's input, attempt to convert the contents of a text box to a number using one of the TryParse methods. If the value of success is "False," you will know that the user's input is not a valid number.

References

  • Photo Credit Jupiterimages/Photos.com/Getty Images
Promoted By Zergnet

Comments

Resources

You May Also Like

  • How to Use Visual Basic

    Visual Basic is a programming language like C++ or Java. Computer programmers use Visual Basic to write Window-based programs. You can use...

  • How to Convert a String to a Number in Visual Basic

    Visual Basic (VB) is a programming language used to create desktop and website applications. One common part of VB programming is converting...

  • How to Use Goal Seek in Visual Basic

    One of Microsoft Excel's most powerful features is "Goal Seek"--the ability to find a value for a cell that produces a certain...

  • Visual Studio SQL Tutorial

    SQL Server is a system that allows developers to create and manage databases. It runs as a service and has no user...

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!