Is there a better way to get my settings?

I am not an IT professional, so I apologize if I missed something obvious.

When writing a program, I add the SettingsIni class, which reads a text file of keys and values. I think this method is really flexible, because the parameters can be added or changed without changing any code, regardless of which application I bound it to. Here is the main code.

 Public Shared Sub Load() Using settingsReader As StreamReader = New StreamReader(System.AppDomain.CurrentDomain.BaseDirectory & "settings.ini") Do While settingsReader.Peek > -1 Dim line As String = settingsReader.ReadLine Dim keysAndValues() As String = line.Split("="c) settingsTable.Add(keysAndValues(0).Trim, keysAndValues(1).Trim) Loop End Using End Sub Public Shared Function GetValue(ByVal key As String) Dim value As String = settingsTable(key) Return value End Function 

This allows you to use the parameter in your code by calling the SettingsIni.GetValue method.

For instance:

 watcher = New FileSystemWatcher(SettingsIni.GetValue("inputDir"), "*" & SettingsIni.GetValue("extn")). 

I find this makes my code readable. My problem is the values ​​in this case, inputDir and extn , are manually typed and are not checked by intellisense. I am always worried that I can make a typo in the rarely used application branch and skip it during testing. Is there a best practice method for retrieving settings? or a way around these unverified values ​​of imprinted characters?

+4
source share
4 answers

The best practice for your sample code is to use constants for possible settings.

 Class Settings Const inputDir as String = "inputDir" Const extn as String = "extn" End Class watcher = New FileSystemWatcher(SettingsIni.GetValue(Settings.inputDir), "*" & SettingsIni.GetValue(Settings.extn)) 
0
source

If this is a number, you can do these 3 things:

  • Check if a numeric value is - using the IsNumeric function
  • Check if this is an integer - using the Int function, for example: if Int (number) = number
  • Check the valid range, for example: if number> = lowerbound and number <= upperbound
0
source

I assume you are using VB.NET? If so, in "my project" there is a convenient menu "Settings". It offers a way to save the settings for your program and get them through "my.settings.YOURKEY". The advantage is that securtiy is applied at this level. In addition, you can also store "resources" in much the same way, but resources are better for strings / images, etc. But they are very effective if you want to translate your program.

As for your current problem: Save the path in the settings, so you do not need to change alll code, but you can use your system and never be mistaken.

0
source

It is completely up to you. You can check almost all things inside quotes, not intellisense.

But you still use the Try-Catch block:

 Try Dim value As String = settingsTable(key) Return value Catch ex As Exception MsgBox(ex.ToString) Return "" End Try 

Thus, you will receive a message box if you are trying to access non-existent settings that you might not understand.

0
source

All Articles