在 VB.net 中从用户那里获取布尔输入



我有一个变量,它是一个布尔数据类型,使用Windows控制台,我希望在其中存储使用输入。我知道如何使用 if 语句和数据验证来做到这一点,但我正在寻找 vb 是否有一种自然处理这个问题的方法?

为了显示一些代码:

Dim tOrF As Boolean
tOrF = Console.ReadLine

谢谢

您可以使用

TryParse方法来检查输入的值是否是有效的布尔值,否则它将引发异常,

尝试转换逻辑的指定字符串表示形式 值到其布尔等价物。返回值指示是否 转换成功或失败。

     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          
你可以

Boolean.Parse(Console.ReadLine())

如果用户不键入 TrueFalse,这将引发异常。

早在 VB6 中,当我们想要满足调整注册表项的人时,我创建了CRegBool(请注意,这是来自Option Compare Text模块):

'----------------------------------------------------------------------
' Function: CRegBool
'
' Purpose:
'   Converts settings value that could have been 'adjusted' by a human to a Boolean.
'
' Arguments:
'   Variant     Value to convert to Boolean.
'   Boolean(Opt)Default value.
'
' Returns:
'   Boolean     True if the value represents an 'affirmative' or non-zero value.
'               False if the value represents a 'negative' or zero value.
'               Otherwise returns default value.
'
' Errors:
'   Only those thrown by CStr (or LCase$).
'
' Notes:
'   Default value should probably never be False unless the Else Case is expanded
' to catch Val(rv) <> 0 -> True; Nevertheless, it is False rather a lot...
'
'   Use LCase$ if Option Compare Binary in operation.
'
' Revision History:
'   070615 MEH Moved from MsgU:modMsgUI.
'   070907 MEH Added commentary.
'   070928 MEH Updated commentary to highlight LCase$ is needed if not Option Compare Text.
'----------------------------------------------------------------------
Public Function CRegBool(ByVal RegValue As Variant, Optional ByVal DefaultValue As Boolean = True) As Boolean
  Select Case CStr(RegValue) 'LCase$(CStr(RegValue)) '
  Case "0", "00", "0x0", "&h0", "false", "no", "off", "n"
    CRegBool = False
  Case "1", "01", "0x1", "&h1", "true", "yes", "on", "-1", "y"
    CRegBool = True
  Case Else
    CRegBool = DefaultValue
  End Select
End Function

使用我最新的已知问题(特别是大写字母稍微好一点,比较起来比较)的快速 VB.NET 转换:

'''----------------------------------------------------------------------
''' Function: CRegBool
'''
''' <summary>
'''   Converts settings value that could have been 'adjusted' by a human to a Boolean.
''' </summary>
'''
''' <parameter name="">Value to convert to Boolean.</parameter>
''' <parameter name="">Default value.</parameter>
'''
''' <returns>
'''   True if the value represents an 'affirmative' or non-zero value.
'''   False if the value represents a 'negative' or zero value.
'''   Otherwise returns default value.
''' </returns>
'''
''' <remarks>
'''   Default value should probably never be False unless the Else Case is expanded
'''  to catch Val(rv) <> 0 -> True; Nevertheless, it is False rather a lot...
'''
'''   Use UCase if Option Compare Binary in operation.
''' </remarks>
'''
''' <revisionhistory>
'''   070615 MEH Moved from MsgU:modMsgUI.
'''   070907 MEH Added commentary.
'''   070928 MEH Updated commentary to highlight UCase is needed if not Option Compare Text.
'''   120924 MEH Converted to VB.NET in the SO text box without testing...
''' </revisionhistory>
'''----------------------------------------------------------------------
Public Function CRegBool(ByVal RegValue As Object, Optional ByVal DefaultValue As Boolean = True) As Boolean
  Select Case CStr(RegValue) 'UCase(CStr(RegValue)) '
  Case "0", "00", "0X0", "&H0", "FALSE", "NO", "OFF", "N"
    CRegBool = False
  Case "1", "01", "0X1", "&H1", "TRUE", "YES", "ON", "-1", "Y"
    CRegBool = True
  Case Else
    CRegBool = DefaultValue
  End Select
End Function

相关内容

最新更新