3

I am currently making a program (C# .Net 4) that has multiple options, which are saved to a file.

These options are their own variables in-code, and I was wondering if there was a way to get the variables and values of these options dynamically in code.

In my case, I have these options in a "Settings" class, and I access them from my main form class using Settings.varSetting.

I get and set these variables in multiple places in code; is it possible to consolidate the list of variables so that I can access and set them (for example, creating a Settings form which pulls the available options and their values and draws the form dynamically) more easily/consistently?

Here are the current variables I have in the Settings class:

    public static Uri uriHomePage = new Uri("http://www.google.com");
    public static int intInitOpacity = 100;
    public static string strWindowTitle = "OpaciBrowser";
    public static bool boolSaveHistory = false;
    public static bool boolAutoRemoveTask = true; //Automatically remove window from task bar if under:
    public static int intRemoveTaskLevel = 50; //percent
    public static bool boolHideOnMinimized = true;

Thanks for any help,

Karl Tatom ( TheMusiKid )

3 Answers 3

5

You might want to consider using the Application Settings features built into the framework for loading and storing application settings.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. A while ago for a different application I was making, I did use Application Settings to store and read user options. However the application (as well as the one I'm currently making) is intended for portable use, which is something Application Settings cannot do very well as far as I know.
Then again I did just find this: stackoverflow.com/questions/4897525/… so maybe that will help.
@TheMusiKid Yeah - the settings infrastructure allows you to put in a custom provider, which will cause storage to do anything you want. It's quite flexible.
L.B's answer did pertain more to what I asked, but after reading this I think I might go ahead and switch over to Application Settings instead; that'd probably make things easier in the long run anyways.
4
var dict = typeof(Settings)
    .GetFields(BindingFlags.Static | BindingFlags.Public)
    .ToDictionary(f=>f.Name, f=>f.GetValue(null));

4 Comments

Thank you for the reply. Unfortunately I don't even know what using; directives to call to make that work and trying to search those out is a pain. Otherwise I'd try it xD.
No need to be snark. I did try it, so that was bad wording on my part. What I mean is, "I don't know what I'm missing to make it work, or I would try to use that method of acquiring the information I need." i.imgur.com/e7TcQ.png
Oh hey, look at that. Didn't even know that was there. Thanks a bunch!
Just parsed it and it's exactly what I needed for it to work. Thanks again.
1

read about reflections: http://msdn.microsoft.com/en-us/library/ms173183%28v=vs.100%29.aspx

1 Comment

Yep. What I needed did definitely relate to the Reflections feature. L.B's answer was just a bit more specific. Thanks though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.