Export (0) Print
Expand All

Convert::ToBoolean Method (Object)

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 nullptr.

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 nullptr, the method returns false.

ExceptionCondition
FormatException

value is a string that does not equal TrueString or FalseString.

InvalidCastException

value does not implement the IConvertible interface.

-or-

The conversion of value to a Boolean is not supported.

The following example converts an array of object values to Boolean values.

array<Object^>^ objects = gcnew array<Object^> { 16.33, -24, 0, "12", "12.7", String::Empty, 
                                                "1String", "True", "false", nullptr, 
                                                gcnew System::Collections::ArrayList };

for each (Object^ obj in objects)
{
   Console::Write("{0,-40}  -->  ", 
                 obj == nullptr ? "null" :
                 String::Format("{0} ({1})", obj, obj->GetType()->Name));
   try {
      Console::WriteLine("{0}", Convert::ToBoolean((Object^) obj));
   }   
   catch (FormatException^) {
      Console::WriteLine("Bad Format");
   }   
   catch (InvalidCastException^) {
      Console::WriteLine("No Conversion");
   }   
}     
// The example displays the following output: 
//       16.33 (Double)                            -->  True 
//       -24 (Int32)                               -->  True 
//       0 (Int32)                                 -->  False 
//       12 (String)                               -->  Bad Format 
//       12.7 (String)                             -->  Bad Format 
//        (String)                                 -->  Bad Format 
//       1String (String)                          -->  Bad Format 
//       True (String)                             -->  True 
//       false (String)                            -->  False 
//       null                                      -->  False 
//       System.Collections.ArrayList (ArrayList)  -->  No Conversion

.NET Framework

Supported in: 4.5.2, 4.5.1, 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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

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.

Show:
© 2014 Microsoft