我有一个 bool 数据类型的变量,并使用 Windows 控制台我希望在其中存储使用输入。我知道如何使用 if 语句和数据验证来做到这一点,但我想看看 vb 是否有一种方法可以自然地处理这个问题?
为了显示一些代码:
Dim tOrF As Boolean
tOrF = Console.ReadLine
谢谢
最佳答案
您可以使用TryParse
方法检查输入的值是否是有效的 bool 值,否则会抛出异常,
Tries to convert the specified string representation of a logical value to its Boolean equivalent. A return value indicates whether the conversion succeeded or failed.
Dim flag As Boolean
Dim value as String = Console.ReadLine()
If Boolean.TryParse(value, flag) Then
Console.WriteLine("'{0}' --> {1}", value, flag)
Else
Console.WriteLine("Unable to parse '{0}'.",
If(value Is Nothing, "<null>", value))
End If
关于vb.net - 在 VB.net 中获取用户的 bool 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12553704/