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