Convert.ToBoolean Method (String, IFormatProvider)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Converts the specified String representation of a logical value to its Boolean equivalent using the specified culture-specific formatting information.
Assembly: mscorlib (in mscorlib.dll)
'Declaration Public Shared Function ToBoolean ( _ value As String, _ provider As IFormatProvider _ ) As Boolean
Parameters
- value
- Type: System.String
A string that contains the value of either TrueString or FalseString.
- provider
- Type: System.IFormatProvider
(Reserved) An IFormatProvider interface implementation that supplies culture-specific formatting information.
Return Value
Type: System.Booleantrue if value equals TrueString, or false if value equals FalseString or Nothing.
| Exception | Condition |
|---|---|
| FormatException | value is not equal to TrueString or FalseString. |
provider is ignored; it does not participate in this operation.
If you prefer not to handle an exception if the conversion fails, you can call the Boolean.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.
The following code sample illustrates the use of ToBoolean, taking a String parameter.
Public Sub ConvertStringBoolean(ByVal stringVal As String) Dim boolVal As Boolean = False Try boolVal = System.Convert.ToBoolean(stringVal) If boolVal Then outputBlock.Text &= String.Format( _ "String is equal to System.Boolean.TrueString.") & vbCrLf Else outputBlock.Text &= String.Format( _ "String is equal to System.Boolean.FalseString.") & vbCrLf End If Catch exception As System.FormatException outputBlock.Text &= String.Format( _ "The string must equal System.Boolean.TrueString " + _ "or System.Boolean.FalseString.") & vbCrLf End Try ' A conversion from bool to string will always succeed. stringVal = System.Convert.ToString(boolVal) outputBlock.Text &= String.Format("{0} as a String is {1}", _ boolVal, stringVal) & vbCrLf End Sub