ARTICLE

Convert string to integer in vb.net

Posted by Sapna Articles | Visual Basic Language June 29, 2011
Convert string to integer value by using Convet.ToInt32 (string) method or int.Parse (string) method
Download Files:
 
Reader Level:

You can easily convert string to integer value by using Convet.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

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor