Partager via


Boolean.Parse Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: October 2010

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

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

Syntax

'Declaration
Public Shared Function Parse ( _
    value As String _
) As Boolean
public static bool Parse(
    string value
)

Parameters

  • value
    Type: System.String
    A string containing the value to convert.

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

value is nulla null reference (Nothing in Visual Basic).

FormatException

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

Remarks

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

Examples

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 Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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)
            outputBlock.Text += String.Format("'{0}' --> {1}", value, flag) & vbCrLf
         Catch e As ArgumentException
            outputBlock.Text &= "Cannot parse a null string." & vbCrLf
         Catch e As FormatException
            outputBlock.Text += String.Format("Cannot parse '{0}'.", value) & vbCrLf
         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'.
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string[] values = { null, String.Empty, "True", "False", 
                          "true", "false", "    true    ", "0", 
                          "1", "-1", "string" };
      foreach (var value in values)
      {
         try
         {
            bool flag = Boolean.Parse(value);
            outputBlock.Text += String.Format("'{0}' --> {1}", value, flag) + "\n";
         }
         catch (ArgumentException)
         {
            outputBlock.Text += "Cannot parse a null string." + "\n";
         }
         catch (FormatException)
         {
            outputBlock.Text += String.Format("Cannot parse '{0}'.", value) + "\n";
         }
      }
   }
}
// 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'.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

October 2010

Revised extensively and replaced the example.

Customer feedback.

June 2010

Corrected return value information.

Customer feedback.