ToBoolean Method (Object)

Convert.ToBoolean Method (Object)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Converts the value of a specified Object to an equivalent Boolean value.

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

public static bool ToBoolean(
	Object value
)

Parameters

value
Type: System.Object
An Object that implements the IConvertible interface or null.

Return Value

Type: System.Boolean
true or false, which reflects the value returned by invoking the IConvertible.ToBoolean method for the underlying type of value. If value is null, the method returns false.

The following code sample illustrates the use of the ToBoolean method, converting a string value to a Boolean type.


public void ConvertStringBoolean(string stringVal)
{
   bool boolVal = false;

   try
   {
      boolVal = System.Convert.ToBoolean(stringVal);
      if (boolVal)
      {
         outputBlock.Text += String.Format(
            "String was equal to System.Boolean.TrueString.") + "\n";
      }
      else
      {
         outputBlock.Text += String.Format(
            "String was equal to System.Boolean.FalseString.") + "\n";
      }
   }
   catch (System.FormatException)
   {
      outputBlock.Text += String.Format(
         "The string must equal System.Boolean.TrueString " +
         "or System.Boolean.FalseString.") + "\n";
   }

   // A conversion from bool to string will always succeed.
   stringVal = System.Convert.ToString(boolVal);
   outputBlock.Text += String.Format("{0} as a string is {1}",
      boolVal, stringVal) + "\n";
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft