Question

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?

No correct solution

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top