["\$ parse.vb~.C\$ ","H.EreEfCrBrCa*/CC|F6R96}.CCCP6R96}X#CEP6R96}aCPFFR1~CP66576}3~E~CP7777765745794F55]~EECX","Integer.Parse."," A String sometimes contains digits. It is converted to an Integer in VB.NET with Integer.Parse. This is a powerful function.","Several Functions."," There are several Integer parsing Functions in the .NET Framework. We see methods and tips for converting Strings to Integers.","First example."," We see a simple example that converts a String containing digits into an actual Integer value. An Integer is often more convenient for a program. ","We declare a String with the contents \"42.\" This is not an Integer, but a representation of two digits in character form.","Result: ","Integer.Parse receives the text string and returns the Integer form of it."," .NET 4.6\n\n","VB.NET program that uses Integer.Parse","\n\nModule Module1\n Sub Main()","\n ' Your input string.\n ","Dim text As String = ","\"42\"","\n\n ' Convert string to integer value.\n ","Dim value As Integer = ","Integer.Parse","(text)","\n\n ' Write output.\n ","Console.WriteLine(value)\n End Sub\nEnd Module\n\n","\n\n42","Integer.TryParse."," This is a programmatic way to both test for integers and 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.","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.","VB.NET program that uses TryParse","\n\nModule Module1\n Sub Main()","\n ' An invalid number string.\n ","Dim s As String = ","\"x\"","\n\n ' Try to parse it.\n ' ... If it isn't a number, use -1.\n ","Dim num As Integer\n If Not ","Integer.TryParse","(s, num) Then\n num = -1\n End If","\n\n ' Writes -1 to the screen.\n ","Console.WriteLine(num)\n End Sub\nEnd Module\n\n","\n\n-1","Using TryParse."," This approach typically simplifies your code. Not only that, but it leads to more robust and crash-proof code. I typically recommend TryParse.","ToInt32."," Here we use Convert.ToInt32 to convert a String to an int. Internally, Convert.ToInt32 is implemented with Integer.Parse. It throws on invalid input. ","The String is converted to an Integer, in the same way as with Integer.Parse. The Integer 500 is written to the Console here.","Console.Write ","console-vbnet","VB.NET program that uses Convert.ToInt32","\n\nModule Module1\n Sub Main()","\n ' The input string you want to convert to Integer.\n ","Dim text As String = ","\"500\"","\n\n ' Convert to an Integer.\n ","Dim value As Integer = ","Convert.ToInt32","(text)","\n\n ' Writes 500 to screen.\n ","Console.WriteLine(value)\n End Sub\nEnd Module\n\n","\n\n500","Benchmark."," Convert.ToInt32 is slower than a call to Integer.Parse. This is because it contains some branch instructions and also has extra method invocation. ","Next: ","To demonstrate, this benchmark times a call to Convert.ToInt32 and Integer.Parse.","Sample that uses Convert.ToInt32: VB.NET","\n\nDim text As String = \"1234\"\nDim i As Integer = Convert.ToInt32(text)\n\n","Sample that uses Integer.Parse: VB.NET","\n\nDim text As String = \"1234\"\nDim i As Integer = Integer.Parse(text)\n\n","Benchmark result, 10 million iterations","\n\n","138.53 ns\n126.76 ns","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. ","Invalid: ","This exception simply means that Integer.Parse was passed a String in an invalid format.","We can handle this problem with exception-handling. But for performance it is better to use Integer.TryParse.","VB.NET program that catches Parse Exception","\n\nModule Module1\n Sub Main()","\n ' First entry try block.\n ","Try","\n ' Parses invalid string and throws.\n ","Dim x As String = ","\"bad\"","\n Dim y As Integer = ","Integer.Parse","(x)\n Catch ex As Exception","\n ' Write exception to screen.\n ","Console.WriteLine(ex)\n End Try\n End Sub\nEnd Module\n\n","\n\nSystem.FormatException: Input string was not in a correct format.","Invalid string benchmark."," With Integer.TryParse we handle invalid strings much faster than with Integer.Parse. Consider this benchmark. We try to parse the string \"xyz.\" ","Version 1: ","The \"A\" function is called and the Integer.Parse method throws an Exception each time.","Exception ","exception-vbnet","Version 2: ","The TryParse method is called. No exceptions are thrown. But the same result is returned.","Result: ","The Integer.TryParse method is many times faster than the Integer.Parse method when dealing with invalid string data.","VB.NET program that benchmarks invalid string parsing","\n\nModule Module1\n\n Sub Main()\n Dim m As Integer = 100000\n Console.WriteLine(","A","())\n Console.WriteLine(","B","())\n\n Dim s1 As Stopwatch = Stopwatch.StartNew\n For i As Integer = 0 To m - 1\n ","A","()\n Next\n s1.Stop()\n\n Dim s2 As Stopwatch = Stopwatch.StartNew\n For i As Integer = 0 To m - 1\n ","B","()\n Next\n s2.Stop()\n\n Dim u As Integer = 1000000\n Console.WriteLine(((s1.Elapsed.TotalMilliseconds * u) / m).ToString(\"0.00 ns\"))\n Console.WriteLine(((s2.Elapsed.TotalMilliseconds * u) / m).ToString(\"0.00 ns\"))\n End Sub\n\n Function ","A","() As Integer","\n ' Use Parse on an invalid string, but handle the exception.\n ","Dim result As Integer\n ","Try","\n result = Integer.","Parse","(","\"xyz\"",")\n ","Catch"," ex As Exception\n result = 0\n End Try\n Return result\n End Function\n\n Function ","B","() As Integer","\n ' Use TryParse on an invalid string.\n ","Dim result As Integer\n Integer.","TryParse","(","\"xyz\"",", result)\n Return result\n End Function\nEnd Module\n\n","Results","\n\n0\n0\n","23907.64 ns"," Integer.Parse, Try, Catch\n"," 49.87 ns"," Integer.TryParse","Tester-doer."," This pattern is used for TryParse. First, the code sees if the parsing will succeed. If this test succeeds, it will do the parse. This avoids costly exceptions. ","Integer: ","Sometimes code that uses Strings where Integers are better. We can convert Strings that contain Integers to actual Integers.","Convert String, Integer ","convert-string-integer-vbnet","Integer ","integer-vbnet","This can improve compatibility. It can lead to improved error-checking. Integers are easier to test.","Parse examples."," Integer.Parse and TryParse are helpful. These functions are the most important and widely-used ones for converting integers to strings."]