Convert.ToBoolean Method (Object)

 

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

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

Public Shared Function ToBoolean (
	value As Object
) As Boolean

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.

Exception Condition
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.

Dim objects() As Object = {16.33, -24, 0, "12", "12.7", String.Empty, _
                           "1String", "True", "false", Nothing, _
                           New System.Collections.ArrayList() }
For Each obj As Object In objects
   If obj IsNot Nothing Then
      Console.Write("{0,-40}  -->  ", _
                    String.Format("{0} ({1})", obj, obj.GetType().Name))
   Else
      Console.Write("{0,-40}  -->  ", "Nothing")   
   End If
   Try
      Console.WriteLine("{0}", Convert.ToBoolean(obj))
   Catch e As FormatException
      Console.WriteLine("Bad Format")
   Catch e As InvalidCastException
      Console.WriteLine("No Conversion")
   End Try   
Next     
' 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
'       Nothing                                   -->  False
'       System.Collections.ArrayList (ArrayList)  -->  No Conversion

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: