Compartir a través de


Convert.ToBoolean Method (String)

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

Converts the specified String representation of a logical value to its Boolean equivalent.

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

Syntax

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

Parameters

Return Value

Type: System.Boolean
true if value equals TrueString, or false if value equals FalseString or nulla null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
FormatException

value is not equal to TrueString or FalseString.

Remarks

For a successful conversion to occur, the value parameter must equal either Boolean.TrueString, a string constant whose value is True, Boolean.FalseString, a string constant whose value is False, or it must be nulla null reference (Nothing in Visual Basic). In comparing value with Boolean.TrueString and Boolean.FalseString, the method ignores case as well as leading and trailing white space.

If you prefer not to handle an exception if the conversion fails, you can call the Boolean.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.

Examples

The following example uses the Convert.ToBoolean(String) method to convert various strings to Boolean values.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ConvertToBoolean(outputBlock, Nothing)
      ConvertToBoolean(outputBlock, String.Empty)
      ConvertToBoolean(outputBlock, "true")
      ConvertToBoolean(outputBlock, "TrueString")
      ConvertToBoolean(outputBlock, "False")
      ConvertToBoolean(outputBlock, "    false    ")
      ConvertToBoolean(outputBlock, "-1")
      ConvertToBoolean(outputBlock, "0")
   End Sub

   Private Sub ConvertToBoolean(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal value As String)
      Try
         outputBlock.Text += String.Format("Converted '{0}' to {1}.", value, _
                           Convert.ToBoolean(value)) & vbCrLf
      Catch e As FormatException
         outputBlock.Text += String.Format("Unable to convert '{0}' to a Boolean.", value) & vbCrLf
      End Try
   End Sub
End Module
' The example displays the following output:
'       Converted '' to False.
'       Unable to convert '' to a Boolean.
'       Converted 'true' to True.
'       Unable to convert 'TrueString' to a Boolean.
'       Converted 'False' to False.
'       Converted '    false    ' to False.
'       Unable to convert '-1' to a Boolean.
'       Unable to convert '0' to a Boolean.
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      ConvertToBoolean(outputBlock, null);
      ConvertToBoolean(outputBlock, String.Empty);
      ConvertToBoolean(outputBlock, "true");
      ConvertToBoolean(outputBlock, "TrueString");
      ConvertToBoolean(outputBlock, "False");
      ConvertToBoolean(outputBlock, "    false    ");
      ConvertToBoolean(outputBlock, "-1");
      ConvertToBoolean(outputBlock, "0");
   }

   private static void ConvertToBoolean(System.Windows.Controls.TextBlock outputBlock, string value)
   {
      try
      {
         outputBlock.Text += String.Format("Converted '{0}' to {1}.", value,
                           Convert.ToBoolean(value)) + "\n";
      }
      catch (FormatException)
      {
         outputBlock.Text += String.Format("Unable to convert '{0}' to a Boolean.", value) + "\n";
      }
   }
}
// The example displays the following output:
//       Converted '' to False.
//       Unable to convert '' to a Boolean.
//       Converted 'true' to True.
//       Unable to convert 'TrueString' to a Boolean.
//       Converted 'False' to False.
//       Converted '    false    ' to False.
//       Unable to convert '-1' to a Boolean.
//       Unable to convert '0' to a Boolean.

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.