Boolean.TryParse Method (String, Boolean)
Tries to convert the specified string representation of a logical value to its Boolean equivalent. A return value indicates whether the conversion succeeded or failed.
Assembly: mscorlib (in mscorlib.dll)
Public Shared Function TryParse ( value As String, <OutAttribute> ByRef result As Boolean ) As Boolean
Parameters
- value
-
Type:
System.String
A string containing the value to convert.
- result
-
Type:
System.Boolean
When this method returns, if the conversion succeeded, contains true if value is equal to Boolean.TrueString or false if value is equal to FalseString. If the conversion failed, contains false. The conversion fails if value is null or is not equal to the value of either the TrueString or FalseString field.
The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails.
The value parameter can be preceded or followed by white space. The comparison is ordinal and case-insensitive.
The following example calls the TryParse method to parse an array of strings. Note that the parse operation succeeds only if the string to be parsed is "True" (the value of the TrueString field) or "False" (the value of the FalseString field) in a case-insensitive comparison.
Module Example Public Sub Main() Dim values() As String = { Nothing, String.Empty, "True", "False", "true", "false", " true ", "0", "1", "-1", "string" } For Each value In values Dim flag As Boolean If Boolean.TryParse(value, flag) Then Console.WriteLine("'{0}' --> {1}", value, flag) Else Console.WriteLine("Unable to parse '{0}'.", If(value Is Nothing, "<null>", value)) End If Next End Sub End Module ' The example displays the following output: ' Unable to parse '<null>'. ' Unable to parse ''. ' 'True' --> True ' 'False' --> False ' 'true' --> True ' 'false' --> False ' ' true ' --> True ' Unable to parse '0'. ' Unable to parse '1'. ' Unable to parse '-1'. ' Unable to parse 'string'.
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1