没有If,.NET可以将“是”和“否”转换为布尔值吗?

时间:2021-11-01 10:00:44

You would think there would be a way using DirectCast, TryCast, CType etc but all of them seem to choke on it e.g.:

你会认为有一种方法可以使用DirectCast,TryCast,CType等,但所有这些似乎都扼杀了它,例如:

CType("Yes", Boolean)

You get:

System.InvalidCastException - Conversion from string "Yes" to type 'Boolean' is not valid.

System.InvalidCastException - 从字符串“是”到“布尔”类型的转换无效。

8 个解决方案

#1


60  

If you think about it, "yes" cannot be converted to bool because it is a language and context specific string.

如果你考虑一下,“是”不能转换为bool,因为它是一个语言和特定于上下文的字符串。

"Yes" is not synonymous with true (especially when your wife says it...!). For things like that you need to convert it yourself; "yes" means "true", "mmmm yeeessss" means "half true, half false, maybe", etc.

“是”不是真的同义词(特别是当你的妻子说出来的时候......)。对于类似的东西,你需要自己转换它; “是”表示“真实”,“mmmm yeeessss”表示“半真,半假,也许”等。

#2


19  

Using this way, you can define conversions from any string you like, to the boolean value you need. 1 is true, 0 is false, obviously.
Benefits: Easily modified. You can add new aliases or remove them very easily.
Cons: Will probably take longer than a simple if. (But if you have multiple alises, it will get hairy)

使用这种方式,您可以将您喜欢的任何字符串的转换定义为所需的布尔值。 1是真的,0显然是假的。好处:易于修改。您可以添加新别名或轻松删除它们。缺点:可能需要比简单的if更长的时间。 (但如果你有多个alises,它会变得毛茸茸)

enum BooleanAliases {
      Yes = 1,
      Aye = 1,
      Cool = 1,
      Naw = 0,
      No = 0
 }
 static bool FromString(string str) {
      return Convert.ToBoolean(Enum.Parse(typeof(BooleanAliases), str));
 }
 // FromString("Yes") = true
 // FromString("No") = false
 // FromString("Cool") = true

#3


5  

No, but you could do like:

不,但你可以这样做:

bool yes = "Yes".equals(yourString);

bool yes =“是”.equals(yourString);

#4


3  

private static bool GetBool(string condition)
{
    return condition.ToLower() == "yes";
}

GetBool("Yes"); // true
GetBool("No"); // false

Or another approach using extension methods

或者使用扩展方法的另一种方法

public static bool ToBoolean(this string str)
{
    return str.ToLower() == "yes";
}

bool answer = "Yes".ToBoolean(); // true
bool answer = "AnythingOtherThanYes".ToBoolean(); // false

#5


1  

Slightly off topic, but I needed once for one of my classes to display 'Yes/No' instead of 'True/False' in a property grid, so I've implemented YesNoBooleanConverter derived from BooleanConverter and decorating my property with <TypeConverter(GetType(YesNoBooleanConverter))> _...

略微偏离主题,但我需要一次我的一个类在属性网格中显示“是/否”而不是“真/假”,所以我实现了从BooleanConverter派生的YesNoBooleanConverter并用 _... (gettype)装饰我的属性(yesnobooleanconverter))>

#6


1  

You Can't. But you should use it as

你不能。但你应该用它作为

bool result = yourstring.ToLower() == "yes";

#7


-1  

Here is a simple way to get this done.

这是完成这项工作的简单方法。

rv.Complete = If(reader("Complete") = "Yes", True, False)

#8


-2  

Public Function TrueFalseToYesNo(thisValue As Boolean) As String
    Try
        If thisValue Then
            Return "Yes"
        Else
            Return "No"
        End If
    Catch ex As Exception
        Return thisValue.ToString
    End Try
End Function

#1


60  

If you think about it, "yes" cannot be converted to bool because it is a language and context specific string.

如果你考虑一下,“是”不能转换为bool,因为它是一个语言和特定于上下文的字符串。

"Yes" is not synonymous with true (especially when your wife says it...!). For things like that you need to convert it yourself; "yes" means "true", "mmmm yeeessss" means "half true, half false, maybe", etc.

“是”不是真的同义词(特别是当你的妻子说出来的时候......)。对于类似的东西,你需要自己转换它; “是”表示“真实”,“mmmm yeeessss”表示“半真,半假,也许”等。

#2


19  

Using this way, you can define conversions from any string you like, to the boolean value you need. 1 is true, 0 is false, obviously.
Benefits: Easily modified. You can add new aliases or remove them very easily.
Cons: Will probably take longer than a simple if. (But if you have multiple alises, it will get hairy)

使用这种方式,您可以将您喜欢的任何字符串的转换定义为所需的布尔值。 1是真的,0显然是假的。好处:易于修改。您可以添加新别名或轻松删除它们。缺点:可能需要比简单的if更长的时间。 (但如果你有多个alises,它会变得毛茸茸)

enum BooleanAliases {
      Yes = 1,
      Aye = 1,
      Cool = 1,
      Naw = 0,
      No = 0
 }
 static bool FromString(string str) {
      return Convert.ToBoolean(Enum.Parse(typeof(BooleanAliases), str));
 }
 // FromString("Yes") = true
 // FromString("No") = false
 // FromString("Cool") = true

#3


5  

No, but you could do like:

不,但你可以这样做:

bool yes = "Yes".equals(yourString);

bool yes =“是”.equals(yourString);

#4


3  

private static bool GetBool(string condition)
{
    return condition.ToLower() == "yes";
}

GetBool("Yes"); // true
GetBool("No"); // false

Or another approach using extension methods

或者使用扩展方法的另一种方法

public static bool ToBoolean(this string str)
{
    return str.ToLower() == "yes";
}

bool answer = "Yes".ToBoolean(); // true
bool answer = "AnythingOtherThanYes".ToBoolean(); // false

#5


1  

Slightly off topic, but I needed once for one of my classes to display 'Yes/No' instead of 'True/False' in a property grid, so I've implemented YesNoBooleanConverter derived from BooleanConverter and decorating my property with <TypeConverter(GetType(YesNoBooleanConverter))> _...

略微偏离主题,但我需要一次我的一个类在属性网格中显示“是/否”而不是“真/假”,所以我实现了从BooleanConverter派生的YesNoBooleanConverter并用 _... (gettype)装饰我的属性(yesnobooleanconverter))>

#6


1  

You Can't. But you should use it as

你不能。但你应该用它作为

bool result = yourstring.ToLower() == "yes";

#7


-1  

Here is a simple way to get this done.

这是完成这项工作的简单方法。

rv.Complete = If(reader("Complete") = "Yes", True, False)

#8


-2  

Public Function TrueFalseToYesNo(thisValue As Boolean) As String
    Try
        If thisValue Then
            Return "Yes"
        Else
            Return "No"
        End If
    Catch ex As Exception
        Return thisValue.ToString
    End Try
End Function