views:

57

answers:

4

I am writing a program in C#. The program controls a physical filter wheel which has interchangable wheels A through H. Each wheel can hold 8 filters. I want the user to be able to store friendly names for each filter in each wheel (64 total). The device and program are capable of identifying the wheel ID(A-H) and filter position (1-8) so what is the best way to store these 64 names and be able to reference them by the ID and POS. I could create a user setting for each one but there are two problems with that: 1) The names would not be the same for every user that logs onto the machine(i think), and 2) in order to access the specific names programmatically I have to use a HUGE case statement. Or is there a way to access settings by the name? like this..?

char WheelID = 'A';
int FilterPos = 4;
NewName = "FriendlyName";

string SettingIWant = WheelID.ToString() + FilterPos.ToString();

Properties.Settings[SettingIWant].Text = NewName;
A: 

Sure there is. Just override the [] operator.

Anon.
+1  A: 

Well, you could take the database approach.

An "in memory" solution, would be Dictionaries.

char WheelID = 'A';
int FilterPos = 4;
NewName = "FriendlyName";

string SettingIWant = WheelID.ToString() + FilterPos.ToString();
Dictionary<string, string> properties = new Dictionary<string, string>()
properties.Add(SettingIWant, NewName);

Ant then you can access the data using bracket syntax

properties[SettingIWant]
Rodrigo Garcia
But without a database I can't store those dictionary entries after the application is exited unless I use settings right?
Jordan S
With proper permissions you could write the name associations out to a text file. And if you want to be fully "buzzword compliant" you could use XML. But I wouldn't.
Jeffrey L Whitledge
Actually, I'm fairly sure you can add an object of type Dictionary<string, string> to a settings file. I have added custom objects in the past, so it really ought to work.
Jeffrey L Whitledge
A: 

OK. I just did some minimal research and here's what I think you want to do.

First, implement Rodrigo Garcia's Dicitonary thing, except use System.Collections.Specialized.StringDictionary. This is one of the types that can be added to a settings file.

When setting up your application settings, under Type, select "Browse..." if you don't see it in the list.

And make sure you always write the changes out, if the collection changes!

Jeffrey L Whitledge
A: 

Ok, I was just dumb and overlooked this way accessing the settings...

string SettingToChange = WheelID.ToString() + Position.ToString();
            Settings1.Default[SettingToChange] = NewName;
            Settings1.Default.Save();

It works just fine. The only issue is the stored values won't be the same for every user but they will just have to deal with that!

Jordan S