|
|
Title | Use GetSetting while protecting against bad values in the Registry in Visual Basic 2005 |
Description | This example shows how to use GetSetting while protecting against bad values in the Registry in Visual Basic 2005. This example uses error handling to catch type mismatch errors and use a default value. |
Keywords | GetSetting, SaveSetting, VB.NET, 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
Try
Return CBool(GetSetting(AppName, Section, Key, _
default_value))
Catch ex As Exception
Return default_value
End Try
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.
Public Function GetSettingBooleanExtras(ByVal AppName As _
String, ByVal Section As String, ByVal Key As String, _
ByVal default_value As Boolean, Optional ByVal _
true_values() As String = Nothing, Optional ByVal _
false_values() As String = Nothing) As Boolean
Dim txt As String = GetSetting(AppName, Section, Key, _
default_value)
' Check for alternate values.
If true_values IsNot Nothing Then
For Each true_value As String In true_values
If txt.ToLower() = true_value.ToLower() Then _
Return True
Next true_value
End If
If false_values IsNot Nothing Then
For Each false_value As String In false_values
If txt.ToLower() = false_value.ToLower() Then _
Return False
Next false_value
End If
' Use what we got from the Registry.
Try
Return CBool(GetSetting(AppName, Section, Key, _
default_value))
Catch ex As Exception
Return default_value
End Try
End Function
|
|
|
|
|
|