Posted by: Sourav | June 1, 2015

TryParse in VB.Net


        Dim s1 As String = “100lk”
        Dim a1 As Integer = 0
        Dim b As Boolean = Int64.TryParse(s1, a1)

        Console.WriteLine(a1)
        Dim values() As String = {Nothing, “160519”, “9432.0”, “16,667”,
                               ”   -322   “, “+4302”, “(100);”,
                               “01FA”}

        For Each value In values
            Dim number As Integer

            Dim result As Boolean = Int32.TryParse(value, number)
            If result Then
                Console.WriteLine(“Converted ‘{0}’ to {1}.”, value, number)
            Else
                Console.WriteLine(“Attempted conversion of ‘{0}’ failed.”,
                                  If(value Is Nothing, “<null>”, value))
            End If
        Next

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: