0

VB2010. I am trying to create one routine that will load all settings into a form with maybe like 50 controls. The first case it load all the user defined settings. The second case loads all the app default settings. I recently found that my routine below will not work as both setting classes are the same.

Private Sub LoadSettingsIntoControls(stsType As Integer)
    Dim stsClass As My.MySettings
    Select Case stsType 
        Case 0 ' user-defined
            stsClass = My.Settings
        Case 1 'app default 
            stsClass = My.MySettings.Default 'this is the same as My.Settings
        Case Else
            Throw New Exception("Invalid settings type.")
    End Select

    txtTmpDir.Text = stsClass.TempDir
    txtDataPath.Text = stsClass.DataPath

    '<about 50 more controls> 
End Sub

I also found that to get the app default value for a setting I need something like

    My.Settings.PropertyValues("TempDir").Property.DefaultValue

I've been trying to wrap up both the user-defined and app default settings into one routine but haven't been able to do so. What I would like is something that needs little upkeep in case I change the setting variable names. I've been looking at documentation and samples but haven't found anything solid. Any suggestions?

sinDizzy
  • 1,300
  • 7
  • 28
  • 60

1 Answers1

0

You should define all default properties with a prefix (eg: def_TempDir) Then you can do thomething like:

Enum EN_PropertyType
    User
    Application
End Enum

Sub LoadSettingsIntoControls(typ As EN_PropertyType)
    For Each ctl In Me.Controls
        If ctl.GetType = (New TextBox).GetType Then
            Dim SettingName As String = ""
            Select Case typ
                Case EN_PropertyType.Application
                    SettingName = "def_" & Mid(ctl.name, 4)
                Case EN_PropertyType.User
                    SettingName = Mid(ctl.name, 4)
            End Select
            ctl.Text = My.Settings.Item(SettingName)
        End If
    Next
End Sub
Chris Berlin
  • 744
  • 6
  • 15
  • Can you please explain what your code is supposed to do exactly? The prefix bit is a non-too-orthodox approach but might be OK (in any case, you should state the reasons for a so unorthodox approach: isn't there any property taking care of that?). You are bringing further issues into account whose point is not completely clear (loop through controls); and you are using old VB6 functions (Mid) whose utilisation is highly disadvisable as far as they use a different indexing than what .NET does (all the indices in .NET start from zero and these functions start from 1). – varocarbas Nov 10 '13 at 17:27
  • I am not sure of this answer either. You say "all default properties" as if its a separate variable. By looking in the Settings page of the My Project the variables have a name, type, scope, and Value (which is the default). There is no special naming of the default property. – sinDizzy Nov 11 '13 at 16:04