我有一个变量,它是一个布尔数据类型,并使用Windows控制台,我希望在其中存储使用输入。我知道如何使用if语句和数据验证来做到这一点,但我想看看vb是否有一种方法可以自然地处理这个问题?
为了显示一些代码:
Dim tOrF As Boolean
tOrF = Console.ReadLine
由于
答案 0 :(得分:2)
您可以使用 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
答案 1 :(得分:0)
您可以致电Boolean.Parse(Console.ReadLine())
。
如果用户未键入True
或False
,则会抛出异常。
答案 2 :(得分:0)
当我们想要满足人们调整注册表项时,回到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