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 )

TheMusiKid
  • 33
  • 1
  • 7

3 Answers3

5

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

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 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. – TheMusiKid Sep 03 '12 at 17:46
  • Then again I did just find this: http://stackoverflow.com/questions/4897525/store-user-settings-into-application-folder so maybe that will help. – TheMusiKid Sep 03 '12 at 18:00
  • @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. – Reed Copsey Sep 03 '12 at 18:01
  • 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. – TheMusiKid Sep 03 '12 at 18:25
4
var dict = typeof(Settings)
    .GetFields(BindingFlags.Static | BindingFlags.Public)
    .ToDictionary(f=>f.Name, f=>f.GetValue(null));
L.B
  • 114,136
  • 19
  • 178
  • 224
  • 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. – TheMusiKid Sep 03 '12 at 17:49
  • 1
    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." http://i.imgur.com/e7TcQ.png – TheMusiKid Sep 03 '12 at 18:15
  • Oh hey, look at that. Didn't even know that was there. Thanks a bunch! – TheMusiKid Sep 03 '12 at 18:16
  • Just parsed it and it's exactly what I needed for it to work. Thanks again. – TheMusiKid Sep 03 '12 at 18:21
1

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

Timoteo Brasil
  • 98
  • 3
  • 12
  • Yep. What I needed did definitely relate to the Reflections feature. L.B's answer was just a bit more specific. Thanks though :) – TheMusiKid Sep 03 '12 at 18:34