我有一个布尔数据类型的变量,并使用 Windows 控制台我希望在其中存储使用输入。我知道如何使用 if 语句和数据验证来做到这一点,但我想看看 vb 是否有一种方法可以自然地处理这个问题?
为了显示一些代码:
Dim tOrF As Boolean
tOrF = Console.ReadLine
谢谢
我有一个布尔数据类型的变量,并使用 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())
。
如果用户不键入True
或,这将引发异常False
。
回到 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 转换,使用我最新的已知问题(特别是 UPPERCASE 比较好):
'''----------------------------------------------------------------------
''' 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