Export (0) Print
Expand All

Convert.ToString Method (Boolean)

.NET Framework 1.1

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

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

Parameters

value
A Boolean value.

Return Value

The String equivalent of the value of value.

Remarks

This implementation is identical to Boolean.ToString.

Example

[Visual Basic, C#, C++] The following code sample illustrates the conversion of a Boolean to a String, using ToString.

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

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Convert Class | Convert Members | System Namespace | Convert.ToString Overload List

Show:
© 2014 Microsoft