Export (0) Print
Expand All

Convert.ToBoolean Method

Converts a specified value to an equivalent Boolean value.

Overload List

Returns the specified Boolean value; no actual conversion is performed.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(Boolean) As Boolean
[C#] public static bool ToBoolean(bool);
[C++] public: static bool ToBoolean(bool);
[JScript] public static function ToBoolean(Boolean) : Boolean;

Converts the value of the specified 8-bit unsigned integer to an equivalent Boolean value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(Byte) As Boolean
[C#] public static bool ToBoolean(byte);
[C++] public: static bool ToBoolean(unsigned char);
[JScript] public static function ToBoolean(Byte) : Boolean;

Calling this method always throws InvalidCastException.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(Char) As Boolean
[C#] public static bool ToBoolean(char);
[C++] public: static bool ToBoolean(__wchar_t);
[JScript] public static function ToBoolean(Char) : Boolean;

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToBoolean(DateTime) As Boolean
[C#] public static bool ToBoolean(DateTime);
[C++] public: static bool ToBoolean(DateTime);
[JScript] public static function ToBoolean(DateTime) : Boolean;

Converts the value of the specified Decimal number to an equivalent Boolean value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(Decimal) As Boolean
[C#] public static bool ToBoolean(decimal);
[C++] public: static bool ToBoolean(Decimal);
[JScript] public static function ToBoolean(Decimal) : Boolean;

Converts the value of the specified double-precision floating point number to an equivalent Boolean value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(Double) As Boolean
[C#] public static bool ToBoolean(double);
[C++] public: static bool ToBoolean(double);
[JScript] public static function ToBoolean(double) : Boolean;

Converts the value of the specified 16-bit signed integer to an equivalent Boolean value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(Short) As Boolean
[C#] public static bool ToBoolean(short);
[C++] public: static bool ToBoolean(short);
[JScript] public static function ToBoolean(Int16) : Boolean;

Converts the value of the specified 32-bit signed integer to an equivalent Boolean value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(Integer) As Boolean
[C#] public static bool ToBoolean(int);
[C++] public: static bool ToBoolean(int);
[JScript] public static function ToBoolean(int) : Boolean;

Converts the value of the specified 64-bit signed integer to an equivalent Boolean value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(Long) As Boolean
[C#] public static bool ToBoolean(long);
[C++] public: static bool ToBoolean(__int64);
[JScript] public static function ToBoolean(long) : Boolean;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(Object) As Boolean
[C#] public static bool ToBoolean(object);
[C++] public: static bool ToBoolean(Object*);
[JScript] public static function ToBoolean(Object) : Boolean;

Converts the value of the specified 8-bit signed integer to an equivalent Boolean value. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(SByte) As Boolean
[C#] public static bool ToBoolean(sbyte);
[C++] public: static bool ToBoolean(char);
[JScript] public static function ToBoolean(SByte) : Boolean;

Converts the value of the specified single-precision floating point number to an equivalent Boolean value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(Single) As Boolean
[C#] public static bool ToBoolean(float);
[C++] public: static bool ToBoolean(float);
[JScript] public static function ToBoolean(float) : Boolean;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(String) As Boolean
[C#] public static bool ToBoolean(string);
[C++] public: static bool ToBoolean(String*);
[JScript] public static function ToBoolean(String) : Boolean;

Converts the value of the specified 16-bit unsigned integer to an equivalent Boolean value. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(UInt16) As Boolean
[C#] public static bool ToBoolean(ushort);
[C++] public: static bool ToBoolean(unsigned short);
[JScript] public static function ToBoolean(UInt16) : Boolean;

Converts the value of the specified 32-bit unsigned integer to an equivalent Boolean value. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(UInt32) As Boolean
[C#] public static bool ToBoolean(uint);
[C++] public: static bool ToBoolean(unsigned int);
[JScript] public static function ToBoolean(UInt32) : Boolean;

Converts the value of the specified 64-bit unsigned integer to an equivalent Boolean value. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(UInt64) As Boolean
[C#] public static bool ToBoolean(ulong);
[C++] public: static bool ToBoolean(unsigned __int64);
[JScript] public static function ToBoolean(UInt64) : Boolean;

Converts the value of the specified Object to an equivalent Boolean value using the specified culture-specific formatting information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(Object, IFormatProvider) As Boolean
[C#] public static bool ToBoolean(object, IFormatProvider);
[C++] public: static bool ToBoolean(Object*, IFormatProvider*);
[JScript] public static function ToBoolean(Object, IFormatProvider) : Boolean;

Converts the specified String representation of a logical value to its Boolean equivalent using the specified culture-specific formatting information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToBoolean(String, IFormatProvider) As Boolean
[C#] public static bool ToBoolean(string, IFormatProvider);
[C++] public: static bool ToBoolean(String*, IFormatProvider*);
[JScript] public static function ToBoolean(String, IFormatProvider) : Boolean;

Example

[Visual Basic, C#, C++] The following code sample illustrates the use of ToBoolean, taking a String parameter.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ToBoolean. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub ConvertStringBoolean(ByVal stringVal As String)

    Dim boolVal As Boolean = False

    Try
        boolVal = System.Convert.ToBoolean(stringVal)
        If boolVal Then
            System.Console.WriteLine( _
                "String is equal to System.Boolean.TrueString.")
        Else
            System.Console.WriteLine( _
                "String is equal to System.Boolean.FalseString.")
        End If
    Catch exception As System.FormatException
        System.Console.WriteLine( _
            "The string must equal System.Boolean.TrueString " + _
            "or System.Boolean.FalseString.")
    End Try

    ' A conversion from bool to string will always succeed.
    stringVal = System.Convert.ToString(boolVal)
    System.Console.WriteLine("{0} as a String is {1}", _
                              boolVal, stringVal)
End Sub

[C#] 
public void ConvertStringBoolean(string stringVal) {
    
    bool boolVal = false;

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

    // A conversion from bool to string will always succeed.
    stringVal = System.Convert.ToString(boolVal);
    System.Console.WriteLine("{0} as a string is {1}",
        boolVal, stringVal);
}

[C++] 

   void ConvertStringBoolean(String* stringVal)
   {
      bool boolVal = false;

      try {
         boolVal = System::Convert::ToBoolean(stringVal);
         if (boolVal) {
            System::Console::WriteLine(S"String was equal to System::Boolean::TrueString.");
         } else {
            System::Console::WriteLine(S"String was equal to System::Boolean::FalseString.");
         }
      } catch (System::FormatException*) {
         System::Console::WriteLine(S"The String* must equal System::Boolean::TrueString or System::Boolean::FalseString.");
      }

      // A conversion from bool to String* will always succeed.
      stringVal = System::Convert::ToString(boolVal);
      System::Console::WriteLine(S" {0} as a String* is {1}",
         __box(boolVal), stringVal);
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Convert Class | Convert Members | System Namespace

Show:
© 2015 Microsoft