VB.NET Integer.Parse

Numbers

A String sometimes contains digits. It is converted to an Integer in VB.NET with Integer.Parse. There are several Integer parsing Functions in the .NET Framework. We see methods and tips for converting Strings to Integers.

Strings
Input/output of integer parsing

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

Parse

One: 1

Here we see a simple example of how to 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.

Based on:

.NET 4.5

VB.NET program that uses Integer.Parse

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
String type

We declare a String with the contents "42." This is not an Integer, but a representation of two digits in character form. Integer.Parse receives the text string and returns the Integer form of it.

TryParse

Try

The Integer.TryParse method provides a programmatic way to both test for integers as well as parse them in a single pass. When parsing integers, we want to avoid exceptions and gracefully handle errors.

Therefore:To accomplish this, we use the Integer.TryParse shared method. TryParse provides a tester-doer pattern to parsing.

Here:This example calls the Integer.TryParse function, which will not throw an exception if the input string is invalid.

Instead:It will return false. The example further shows how to test the return value of TryParse.

If Then
VB.NET program that uses TryParse

Module Module1
    Sub Main()
	' An invalid number string
	Dim s As String = "x"

	' Try to parse it. If it isn't a number, use -1
	Dim num As Integer
	If Not Integer.TryParse(s, num) Then
	    num = -1
	End If

	' Writes -1 to the screen
	Console.WriteLine(num)
    End Sub
End Module

Output

-1
Copyright

The first String is not a valid integer. Integer.TryParse then returns False. The If Not statement then assigns the number to -1. The Integer is either set to the number in the String, or -1 if there is no valid number in the String.

Using TryParse:This approach typically simplifies your code. Not only that, but it leads to more robust and crash-proof code.

ToInt32

Int keyword

The next example shows how to 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.

And: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.ToInt32Console
VB.NET program that uses Convert.ToInt32

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

Exceptions

Warning: exclamation mark

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.

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.

VB.NET program that catches Parse Exception

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.

Tester-doer

Cover logo

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.

Note:I have seen code that uses Strings where Integers would be better. You can convert Strings that contain Integers to actual Integers.

And:This can improve compatibility. It can lead to improved error-checking. Integers are easier to test.

Integer

Summary

The VB.NET programming language

We saw 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: Number