Boolean.Parse Method

Converts the specified string representation of a logical value to its Boolean equivalent, or throws an exception if the string is not equal to the value of Boolean.TrueString or Boolean.FalseString.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function Parse ( _
	value As String _
) As Boolean

Parameters

value
Type: System.String

A string containing the value to convert.

Return Value

Type: System.Boolean
true if value is equal to the value of the Boolean.TrueString field; false if value is equal to the value of the Boolean.FalseString field.

ExceptionCondition
ArgumentNullException

value is Nothing.

FormatException

value is not equal to the value of the TrueString or FalseString field.

The value parameter, optionally preceded or trailed by white space, must contain a string that is equal to the value of either the Boolean.TrueString or Boolean.FalseString field; otherwise, a FormatException is thrown. The comparison is ordinal and case-insensitive.

You can call the TryParse method to parse a string without having to handle the FormatException exception that is thrown if the parsing operation fails.

The following example calls the Parse 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
         Try 
            Dim flag As Boolean = Boolean.Parse(value)
            Console.WriteLine("'{0}' --> {1}", value, flag)
         Catch e As ArgumentException
            Console.WriteLine("Cannot parse a null string.")
         Catch e As FormatException
            Console.WriteLine("Cannot parse '{0}'.", value)
         End Try          
      Next                                      
   End Sub 
End Module 
' The example displays the following output: 
'       Cannot parse a null string. 
'       Cannot parse ''. 
'       'True' --> True 
'       'False' --> False 
'       'true' --> True 
'       'false' --> False 
'       '    true    ' --> True 
'       Cannot parse '0'. 
'       Cannot parse '1'. 
'       Cannot parse '-1'. 
'       Cannot parse 'string'.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft