|
|
Title | Use GetSetting while protecting against bad values in the Registry in Visual Basic 6 |
Description | This example shows how to use GetSetting while protecting against bad values in the Registry in Visual Basic 6. This example uses error handling to catch type mismatch errors and use a default value. |
Keywords | GetSetting, SaveSetting, Visual Basic 6, error handling, error trapping |
Categories | Software Engineering, Tips and Tricks, Windows |
|
|
GetSetting returns a value from the Registry or a default value if the indicated key isn't in the Registry. However, if the value in the Registry contains garbage, then you may not be able to use it. For example, consider the following code.
Dim last_ok As Boolean
last_ok = GetSetting("MyApp", "Settings", "LastOk", "True")
If there is no such value in the Registry, then GetSetting returns "True" and the statement works. If there is such a value in the Registry and that value is either True or False, then GetSetting returns it and again everything is fine.
But suppose the Registry contains the value "Verdad." GetSetting returns it and the program crashes because it cannot convert that value into a Boolean.
The GetSettingBoolean function uses GetSetting to get a value that is supposed to be a Boolean. It converts the value returned into a Boolean. If it fails, the function catches the error and returns the default value.
|
|
' Get a Boolean.
Public Function GetSettingBoolean(ByVal AppName As String, _
ByVal Section As String, ByVal Key As String, ByVal _
default_value As Boolean) As Boolean
On Error Resume Next
GetSettingBoolean = CBool(GetSetting(AppName, Section, _
Key, default_value))
If Err.Number <> 0 Then GetSettingBoolean = _
default_value
Err.Clear
End Function
|
|
The example program contains other functions to safely get Integer, String, Long, and Date values. (GetSetting should never fail to get a String value but there's a function for this in the example for completeness.)
Gary Winey provided this excellent enhancement. Sometimes someone stores something unusual in the Registry. For example, Yes and No instead of True and False.
The following function takes optional lists of values that should be accepted as True or False.
|
|
' Get a Boolean with optional extra values.
' Note that optional values in VB 6 must be Variants or
' intrinsic values
' so here true_values and false_values are assuemd to be
' variant arrays.
Public Function GetSettingBooleanExtras(ByVal AppName As _
String, ByVal Section As String, ByVal Key As String, _
ByVal default_value As Boolean, Optional true_values As _
Variant, Optional false_values As Variant) As Boolean
Dim txt As String
Dim i As Integer
txt = GetSetting(AppName, Section, Key, default_value)
' Check for extra values.
If Not IsMissing(true_values) Then
For i = LBound(true_values) To UBound(true_values)
If LCase$(txt) = LCase$(true_values(i)) Then
GetSettingBooleanExtras = True
Exit Function
End If
Next i
End If
If Not IsMissing(false_values) Then
For i = LBound(false_values) To UBound(false_values)
If LCase$(txt) = LCase$(false_values(i)) Then
GetSettingBooleanExtras = False
Exit Function
End If
Next i
End If
' Try to convert whatever we got from the Registry.
On Error Resume Next
GetSettingBooleanExtras = CBool(txt)
If Err.Number <> 0 Then GetSettingBooleanExtras = _
default_value
Err.Clear
End Function
|
|
|
|
|
|