Use an ENUM to save your settings in VB.NET

February 29th, 2012

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 )

Programming in VB.NET Help

July 6th, 2010

Help Compile a VB.NET Programming Directory

I’m always looking for great sites that could help me over this VB6 to VB.Net learning curve. 

Lately, I’ve been having problems getting data from a SQLExpress database to display easily, without doing so much coding and behind the scenes set up.  And nobody, so far, has a good demo of a multi-table,  relational database application that doesn’t show “Customer ID: 56″ instead of “Acme Publishing – San Jose, CA”.   

So I thought if I’m always looking for that next great “Ah-HA” blog, page or site that could better explain VB.Net to me, then other people must be as well.

I need help compiling this resource.  If your the author of a site like this, send me an email. (Steve at carterszoo.com).  Even if you just know of an well authored, educational and informative VB.NET site, send me the link.

Thank you.

Joke – Sound of Humor

July 1st, 2010
The following is humor and should not be taken seriously.
If you can't take a joke don't read any further, turn
off your computer and quickly seek professional
psychiatric help.  Seriously.

See no evil

What goes

                “clip-clop,

                         clip-clop,

                               clip-clop,

                                        bang-bang,

                                              clip-clop,

                                                       clip-clop” ?

 

An Amish Drive-by Shooting.