VB.NET Integer.Parse Function Examples

The VB.NET programming language

You have a String in VB.NET that contains numeric digits, but is not an Integer type. Use one of several Integer parse methods to convert it to an Integer. Here we see methods and tips for converting Strings to Integers.

These VB programs use the Integer.Parse Function. They show Convert.ToInt32.

Input/output of integer parsing

String input:   "42"
		"x"
		"500"
		"-1"
Integer output: 42
		[Invalid]
		500
		-1

Example 1

Here we see a simple example of how you can convert a String that contains digits into an actual Integer type. Having an Integer is much more convenient for the rest of your program, so this is important.

Program that uses Integer.Parse [VB.NET]

Module Module1
    Sub Main()
	' Your input string
	Dim text As String = "42"

	' Convert string to integer value
	Dim value As Integer = Integer.Parse(text)

	' Write output
	Console.WriteLine(value)
    End Sub
End Module

Output

42
Main method

Description. The code defines the Main entry point and declares a String with the contents of "42". This is not an Integer, but a representation of two digits in character form. The Integer.Parse shared function receives the text string and returns the Integer form of it. In this case, the number 42 will be returned.

Integer.TryParse

The Integer.TryParse method provides a programmatic way to both test for integers as well as parse them in a single pass. Because Integer.TryParse is so commonly used, it is described in more detail on a separate page on this site. Please reference the appropriate article.

Integer.TryParse

Example 2

The next example here shows how you can use the return value of Convert.ToInt32 to assign an Integer to the numeric String value. Internally, Convert.ToInt32 is implemented with Integer.Parse. This means it will throw on invalid input.

Program that uses Convert.ToInt32 [VB.NET]

Module Module1
    Sub Main()
	' The input string you want to convert to Integer
	Dim text As String = "500"

	' Convert to an Integer
	Dim value As Integer = Convert.ToInt32(text)

	' Writes 500 to screen
	Console.WriteLine(value)
    End Sub
End Module

Output

500

Explanation of Convert.ToInt32. Here, the String is converted to an Integer, in the same way as with Integer.Parse. The Integer 500 is written to the Console here.

Convert.ToInt32 Function

Exceptions

Here we see what happens when you try to parse a String that does not contain a numeric value as an Integer. You will get a System.FormatException. This may or may not be a disaster for you.

Program that catches Parse Exception [VB.NET]

Module Module1
    Sub Main()
	' First entry try block
	Try
	    ' Parses invalid string and throws
	    Dim x As String = "bad"
	    Dim y As Integer = Integer.Parse(x)
	Catch ex As Exception
	    ' Write exception to screen
	    Console.WriteLine(ex)
	End Try
    End Sub
End Module

Output

System.FormatException: Input string was not in a correct format.

Description. This exception simply means that Integer.Parse was passed a String in an invalid format. You can handle this problem with the style of code above. However, for performance it is better to use Integer.TryParse as shown in the previous example.

Design notes

Note

The tester-doer pattern is how Microsoft implements the TryParse method. First, the code sees if the parsing will succeed. If this test succeeds, it will do the parse. This avoids costly exceptions and excess complexity.

Program design note. Often, I have seen code that uses Strings where Integers would be better. I suggest you convert Strings that contain Integers to actual Integer types. This can improve compatibility and error-checking.

Summary

We saw several examples relating to Integer.Parse in the Visual Basic programming language. Generally, Integer.Parse and Integer.TryParse are the most important and widely-used methods for converting integers and strings in programs.

VB.NET Tutorials
.NET