.NET可以将“是”转换为“是”吗? & “否”;布尔如果没有? [英] Can .NET convert "Yes" & "No" to boolean without If?

查看:77
本文介绍了.NET可以将“是”转换为“是”吗? & “否”;布尔如果没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能会认为有一种使用DirectCast,TryCast,CType等的方法,但是它们似乎都使它窒息,例如:

  CType( Yes,Boolean)

您将获得:


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



解决方案

如果考虑到这一点, yes不能转换为bool,因为它是特定于语言和上下文的字符串。 / p>

是不是true的同义词(尤其是当您的妻子说出来的时候!!)。对于类似的事情,您需要自己进行转换。 yes表示 true, mmmm yeeessss表示一半为true,一半为false,也许,等等。


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

CType("Yes", Boolean)

You get:

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

解决方案

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

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

这篇关于.NET可以将“是”转换为“是”吗? & “否”;布尔如果没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆