How to convert String to Integer using VB.NET

Convert string to integer value by using Convet.ToInt32 (string) method or int.Parse (string) method
  • 3819
Download Files:
 

You can easily convert string to integer value by using Convert.ToInt32 (string) method or int.Parse (string) method both work similar but there is difference in both of them.

Convert.ToInt32 (string): System namespace provide Convert class. It takes object as an argument and it does not through ArgumentNullException when its argument is null value. It will give output 0. It is slower than int.Parse (string).

img.GIF

int.Parse (string):  It is faster then Convert.ToInt32 (string). It returns ArgumentNullException when its argument is null value.

Code

Module Module1
    Sub Main()
        ' First way to convert string to integer
        ' Convert a string to an integer
        Dim text As String = "99"
        Dim stringToInteger As Integer = Convert.ToInt32(text)
        Console.WriteLine("Convert string using Convert.ToInt32(string) : " & stringToInteger & vbLf)

        ' Second way to convert string to integer
        ' Convert string to number.
        Dim text1 As String = "1919"
        Dim stringToInteger1 As Integer = Integer.Parse(text1)
        Console.WriteLine("Convert string using int.Parse(string) : " & stringToInteger1 & vbLf)

        ' Convert a string to a decimal
        Dim stringToDecimal As Decimal = Convert.ToDecimal("19.19")
        Console.WriteLine("Convert decimal using Convert.ToDecimal(string) : " & stringToDecimal & vbLf)

        ' Booleans can be converted too
        Dim stringToBoolean As Boolean = Convert.ToBoolean("true")
        Console.WriteLine("Boolean value : " & stringToBoolean & vbLf)
        Console.ReadLine()
    End Sub
End Module

Output

output.gif

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.