Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Better Method To Handle Global Settings? 1

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
I have a Global table that contains various values such as Default Insurance Cost, Fax Time Minimum, Fax Time Maximum, Email Invoice Delay Days, etc., which are used throughout my program.
The two main columns of the table are VariableName and VariableValue.
I have a global class in the program where I define the variables as Public Shared. Sample Here
Code:
Public Shared InsuranceCost as decimal = cdec(0.0)
I load the class in a loop as follows:
Code:
 dsGlobals = SelectGlobalData()
' Process All Rows
For Counter1 = 0 To dsGlobals.Tables(0).Rows.Count - 1
	Select Case dsGlobals.Tables(0).Rows(Counter1).Item("VarName").ToString.Trim.ToUpper

        Case "InsuranceCost".ToUpper
          ' Insurance Cost
          Globals.InsuranceCost1 = CDec(dsGlobals.Tables(0).Rows(Counter1)("VarValue"))

        Case "FaxTimeMinValue".ToUpper
          ' Fax Allowance in Minutes (Before Reporting Problem, Stop Reporting Problem)
          Globals.FaxTimeMinValue = CInt(dsGlobals.Tables(0).Rows(Counter1)("VarValue"))

        Case "FaxTimeMaxValue".ToUpper
          Globals.FaxTimeMaxValue = CInt(dsGlobals.Tables(0).Rows(Counter1)("VarValue"))

        Case "BolDelayDays".ToUpper
          ' Delay this many days before looking for BOL files
          Globals.BolDelayDays = CInt(dsGlobals.Tables(0).Rows(Counter1)("VarValue"))

	Case ....(More Here)

	End Select
Next
When I need one of the global variables I use this code.
Code:
InsuranceCost = Globals.InsuranceCost
This works, but is there a better way to load and/or use the values of these global settings?
I thought about just having the table in the Globals Class and having a procedure in the class that would find the correct record and return the value needed. I don't think this would take too much time as there are less than 100 records in the table.

Auguy
Sylvania/Toledo Ohio
 
If there are fewer than 100 records in the settings table, I would just load the table into memory (datatable) at program start and use that, so you wouldn't need to go back to the database all the time. You wouldn't even need to load the key/value pairs into variables, you could just filter a dataview of the datatable to get the record needed at any given time. For example, with the dataview named dvGlobals:

dvGlobals.RowFilter = "VariableName='InsuranceCost'

Then to use the value, reference dvGlobals.Item("VariableValue").

You could also turn this into a function.


Alternatively, you could use application settings to store all of your globals. Here's a link to a good article that covers the basics of how to use the application settings:


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks, I guess that was what I was trying to describe in the last few sentences. You're post confirmed it.
I will check out the application settings. Thanks Again!

Auguy
Sylvania/Toledo Ohio
 
Just a guess here, but you may also want to investigate VB.NET's List and/or Dictionary - load it from the table, of course

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top