指定したカルチャに固有の書式情報を使用して、指定した論理値の String 形式を等価の Boolean 型の値に変換します。
Overloads Public Shared Function ToBoolean( _
ByVal value As String, _ ByVal provider As IFormatProvider _) As Boolean
[C#]
public static bool ToBoolean(stringvalue,IFormatProviderprovider);
[C++]
public: static bool ToBoolean(String* value,IFormatProvider* provider);
[JScript]
public static function ToBoolean(
value : String,provider : IFormatProvider) : Boolean;
パラメータ
- value
TrueString または FalseString のいずれかの値を格納する文字列。 - provider
(予約済み) カルチャに固有の書式情報を提供する IFormatProvider インターフェイス実装。
戻り値
value が TrueString に等しい場合は true 。 value が FalseString または null 参照 (Visual Basic では Nothing) に等しい場合は、 false 。
例外
例外の種類 | 条件 |
---|---|
FormatException | value が TrueString または FalseString のいずれとも等しくありません。 |
解説
provider は無視され、この操作には関与しません。
使用例
[Visual Basic, C#, C++] ToBoolean を使用して String パラメータを受け取る方法については、次のコード例を参照してください。
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
Convert クラス | Convert メンバ | System 名前空間 | Convert.ToBoolean オーバーロードの一覧 | String