Reputation: 2180
I have a variable which is a boolean datatype and using the windows console I am looking to store use input inside it. I know how to do this using if statements and data validation, but I am looking to see if vb has a method to handle this naturally?
For the sake of showing some code:
Dim tOrF As Boolean
tOrF = Console.ReadLine
Thanks
Upvotes: 2
Views: 3208
Reputation: 10931
Back in VB6 when we wanted to cater for humans adjusting registry entries, I created CRegBool
(note this was from an Option Compare Text
module):
'----------------------------------------------------------------------
' 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
A quick VB.NET conversion, using my latest known issues (specifically that UPPERCASE is slightly better to compare against):
'''----------------------------------------------------------------------
''' 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
Upvotes: 0
Reputation: 263933
You can use TryParse
method to check if the value being entered is a valid boolean value or else it will throw an exception,
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
Upvotes: 2
Reputation: 888233
You could call Boolean.Parse(Console.ReadLine())
.
This will throw an exception if the user doesn't type True
or False
.
Upvotes: 0