I’m always looking for a way not only to make coding easier but also more useful and understandable. And it doesn’t hurt if helps the documenting process as well.
With that in mind I came up with this method of saving settings that helps keep all your application settings nicely in one location, helps prevent misspelling a setting and keeps those settings in one class or module.
First, let me explain the problems I used to run into with this set of functions:
SaveSetting("Think Hard for a good name", "Main Application", _
"Some Setting", SomeVariable)
And then someplace else in the application you try to read the settings in a you forget half of what you used for the setting name and section and you end up with soemthing like this:
GetSetting("A Good Name is hard to think of", "App Main", _
"Something", SomeVariable)
Which will end up with garbage.
So I’m thinking… how to standardize the saving and getting of settings…mmm…
An ENUM!!!
So Here’s what I have so far, but I think it could be expanded upon…
Public Enum SettingTypes As Integer
InputDir 'These are just examples ExportDir 'But very easy to comment MainTimerInterval 'So you always know MainWindowXLocation 'What each entry is MainWindowYLocation 'Comment the hell MainWindowWidth ' out of each one MainWindowHeight ' get my drift?
End Enum Module MainModule #Region "Methods" Public Function ReadThisSetting(ByVal SettingName As SettingTypes) As String
Dim tmpRead As String = "" Try
tmpRead = GetSetting(My.Application.Info.Title, "Settings", SettingName.ToString) Return tmpRead
Catch ex As Exception
MessageBox.Show("Error Reading Settings" & vbCrLf & vbCrLf & ex.Message, "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return ""
End Try
End Function Public Function SaveThisSetting(ByVal SettingName As SettingTypes, ByVal SettingValue As _ String) As Boolean
Try
SaveSetting(My.Application.Info.Title, "Settings", SettingName.ToString, SettingValue) Return True
Catch ex As Exception
MessageBox.Show("Error Saving Settings" & vbCrLf & vbCrLf & ex.Message, "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function #End Region 'Methods End Module
How to use it
Dim ExportPathString as String = ReadThisSetting(ExportDir)
and…
ExportPathString = "C:\TEMP" SaveThisSetting(ExportDir, ExportPathingString)
More examples
Saving Integer Values
Dim iInterval as integer iInterval = Convert.ToInt32(ReadThisSetting(MainTimerInterval )