Export (0) Print
Expand All

Convert.ToBoolean Method (String, IFormatProvider)

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

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

public static bool ToBoolean (
	string value,
	IFormatProvider provider
)
public static boolean ToBoolean (
	String value, 
	IFormatProvider provider
)
public static function ToBoolean (
	value : String, 
	provider : IFormatProvider
) : boolean

Parameters

value

A string that contains the value of either TrueString or FalseString.

provider

(Reserved) An IFormatProvider interface implementation that supplies culture-specific formatting information.

Return Value

true if value equals TrueString, or false if value equals FalseString or a null reference (Nothing in Visual Basic).

Exception typeCondition

FormatException

value is not equal to TrueString or FalseString.

provider is ignored; it does not participate in this operation.

The following code sample illustrates the use of ToBoolean, taking a String parameter.

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);
}

public void ConvertStringBoolean(String stringVal)
{
    boolean 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 exp) {        
        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}", 
        System.Convert.ToString(boolVal), stringVal);
} //ConvertStringBoolean

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft