Question

I have added more settings to c# project. i have Settings.settings, Settings1.settings, ... each of those have same value settings of many types.. id - string, point - Point(), size - Size() but different values.

example:

Settings.settings has data:

id = 'first'; string point = 10@10; Point() size = 111; 111; Size()

Settings1.settings has data:

id = 'second'; string point = 20@20; Point() size = 222; 222; Size()

I choose which settings the program will be using at start (by choosing id of setting from listbox).

I then want to get settings value for selected setting with:

Object myPoint = Config.currentPropertiesAt("point");

/Config class/ -> public static Object currentPropertiesAt(string value)

How can i get that value here from correct Settings?

How can i know what object type it will get returned? That is why i use Object, but this can not be OK.

What is a better way?

Was it helpful?

Solution

I'd suggest to create an Interface ISettings containing all the settings that you will need:

interface ISettings
{
    string Id { get;  }
    Point Point { get;  }
    Size Size { get;  }

    ...
}

Then define partial classes that will match your generated Settings classes:

internal sealed partial class Settings : ISettings { }
internal sealed partial class Settings1 : ISettings { }
internal sealed partial class Settings2 : ISettings { }
...

Because your generated Settings1.Designer.cs classes implement the actual interface, there is nothing more to add here.

Now, in your code you can always reference ISettings to get to the value of your settings:

var currentSettings = Config.CurrentSettings; // returns ISettings
var id = currentSettings.Id;

In the code where you wish to select which settings (1,2, ...) to use (I assume this is in your Config class) you return the correct class:

class Config
{
    // Select the correct object using a string, or enum, or int, or ...
    public static ISettings SetCurrentSettings(string selectedSettings)
    {
        switch (selectedSettings)
        {
            default:
            case "Settings":
                CurrentSettings = Properties.Settings.Default;
                break;
            case "Settings1":
                CurrentSettings = Properties.Settings1.Default;
                break;
            case "Settings2":
                CurrentSettings = Properties.Settings2.Default;
                break;
        }
        return CurrentSettings;
    }
    public static ISettings CurrentSettings { get; private set; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top