2

I have a string user setting and want to select a particular variable with the same name during the startup of my C# Windows app.

e.g.

I have a user setting (string) called UserSelectedInt, which is currently set to 'MyTwo'. (Please note that my variables are actually a lot more complex types than integers, I've just used them as examples.)

public static int MyOne = 12345;
public static int MyTwo = 54321;
public static int MyThree = 33333;

public int myInt = SelectMyVariableUsing(MyApp.Settings.Default.UserSelectedInt)

The user may have selected 'MyTwo' last time they closed the app, so that is the variable I want to select during startup. I hope I'm making sense.

Please can some let me know how would I achieve this?

Thanks

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
JamesW
  • 849
  • 2
  • 14
  • 27

8 Answers8

23

Use a Dictionary<string, int>. That allows you to assign an integer value to a number of strings. If the string is user input, you need to check that it is valid before trying to retrieve the corresponding integer.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
2

Sounds like you are trying to implement the provider pattern. You may find using this is a better mechanism for you to use, especially as you say it is more complex than using ints.

In your code you would reference the particular provider using your MyApp.Settings.Default.UserSelectedInt setting.

I would say architecturally this would be a better mechanism than some of the other suggested answers.

RichardOD
  • 28,883
  • 9
  • 61
  • 81
2

The simplest way is probably to just use GetField.

Using your example just change the last line to:

var selectedField = MyApp.Settings.Default.UserSelectedInt
public int myInt = (int) GetType().GetField(selectedField).GetValue(this);

If the field or class is static the syntax should be:

var selectedField = MyApp.Settings.Default.UserSelectedInt
public int myInt = (int)typeof(YourClass).GetField(selectedField).GetValue(null);

See http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.getvalue.aspx for more details.

MartinH
  • 21
  • 4
  • I can't get this to compile - I am using C# in VS 2008 (.NET 3.5) - is this code for a different version? It tells me 'An object reference is required for the non-static field, method, or property 'object.GetType()' – JamesW Aug 11 '09 at 10:50
  • This is using reflection. You need to replace (int) with your variable. – James Aug 11 '09 at 11:11
  • Yeah I'm doing that - no joy I'm afraid. I'm inside a static class and therefore can't use 'this' either. Could that be affecting things? – JamesW Aug 11 '09 at 11:18
  • Ah right... I'm going to go with the Dictionary method for now then – JamesW Aug 11 '09 at 11:25
  • 1
    You can still use reflection with static fields, have updated answer to reflect this. HTH Would echo what others have said about design though. Not that familiar with provider pattern but it might be a better way to go – MartinH Aug 11 '09 at 15:52
  • I've tried the updated code and now get 'Object reference not set to an instance of an object'... – JamesW Aug 12 '09 at 10:16
2
// enumerate your list of property names (perhaps from a file)
var settings = new List<string>(); 
settings.Add("MyOne"); 
settings.Add("MyTwo"); 
settings.Add("MyThree");

var settingMap = new Dictionary<string, int>(); 
int value = 0; 
foreach (var name in settings) 
{
    try
    {
        // try to parse the setting as an integer
        if (Int32.TryParse((string)Properties.Settings.Default[name], out value))
        {
            // add map property name to value if successful
            settingMap.Add(name, value);
        }
        else
        {
            // alert if we were unable to parse the setting
            Console.WriteLine(String.Format("The settings property \"{0}\" is not a valid type!", name));
        }
     }
     catch (SettingsPropertyNotFoundException ex)
     {
         // alert if the setting name could not be found
         Console.WriteLine(ex.Message);
     }
}

However, if you get to the stage where your variable list is HUGE then I would maybe look at actually accessing the properties file directly via some form of XML parsing.

James
  • 80,725
  • 18
  • 167
  • 237
0

Should be possible with Reflection

Fredrik Leijon
  • 2,792
  • 18
  • 20
0

You could also use a Hashtable.

However, from the looks of it all your really wanting to do is store the value the user last selected hence I would probably just save it in the UserSettings as a string and then on load I would just parse the value.

James
  • 80,725
  • 18
  • 167
  • 237
0

I hope I am reading this right, but essentially, I feel you want to remember the value user selected last time. Since the application will be shutdown in the meantime, you are going to have to store it somewhere. Maybe in a file.

Tanmay
  • 1,527
  • 1
  • 9
  • 7
0

Too me it sounds more like you should use an enumeration in stead of static variables, consider the following example:

    public enum MyVars
    {
        MyOne = 12345, MyTwo = 54321, MyThree = 33333 
    }
    static void Main(string[] args)
    {
        Console.WriteLine(String.Format("Name:{0}, Val={1}", MyVars.MyOne.ToString(), (int)MyVars.MyOne ));
        Console.ReadKey();
    }

, it will output "Name:MyOne, Val=12345", with enumerations you can easily pick out the name of the variable.