vb.net - 在 VB.net 中获取用户的 bool 输入

标签 vb.net

我有一个 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/

相关文章:

vb.net - 多个 if 语句检查字符串长度并缩短

vb.net - 居中绘制文本

vb.net - 在按下按钮的同时静音除我的以外的所有其他程序

vb.net - 循环遍历对象的属性值?

vb.net - 返回数据集中表的前 5 条记录

vb.net - .NET - 非常奇怪的 NullReferenceException?

c# - 在 ASP.net Web 应用程序中使用多种编程语言

c# - 查找不在数据表中但存在于 List<customclass> 中的项目

asp.net - 使用循环将整数填充到下拉列表

mysql - 在组合框中选择或显示某些行 mysql vb.net