Accessing User Settings via a Variable

N

NJD

Hi,
I have a C# applications that makes use of user settings using the autobuilt
settings class. Using the IDE, I created a couple of user settings such as
Key1=Value1, Key2=Value2 and so on. In my program I have a variable that
holds the name of the user setting I want to read. How do I use the variable
to access the user setting?

The code I have is something like this
string myVar = "Key2";
I want to do something like
string result = MyApp.Properties.Settings.myVar;

Obviously the above line doesn't work because myVar is a variable. How do I
work around this?

Thanks,
NJD
 
J

Jeff Johnson

I have a C# applications that makes use of user settings using the
autobuilt settings class. Using the IDE, I created a couple of user
settings such as Key1=Value1, Key2=Value2 and so on. In my program I have
a variable that holds the name of the user setting I want to read. How do
I use the variable to access the user setting?

The code I have is something like this
string myVar = "Key2";
I want to do something like
string result = MyApp.Properties.Settings.myVar;

Obviously the above line doesn't work because myVar is a variable. How do
I work around this?

A) I think you'd really be wanting

MyApp.Properties.Settings.Default.myVar;

B) Just off the top of my head, does the class returned by the Default
property have an indexer? If so, try:

MyApp.Properties.Settings.Default[myVar];

and see what you get!
 
N

NJD

Hi Jeff,

Thanks for your reply.
-Option A doesn't work. It doesn't see myVar as a member of
"MyApp.Properties.Settings.Default". This is the problem: myVar is not a
member. The value of myVar is however.

-Option B does work. Excellent. The Default property does have an indexer
and hence
MyApp.Properties.Settings.Default[myVar];
works perfectly.

Strangely, the reason I didn't go down this path is because I never got
".Default" when using IntelliSense as an option.
I created a member in my main form of type "MyApp.Properties.Settings" and
then used
mySettings = new MyApp.Properties.Settings();
after the form initialization for easy reference to the settings.
The thing is that when I then use the mySettings reference it does not bring
me ".Default" as member of the class. However I now discovered that there is
an indexer on MyApp.Propertoes.Settings itself.

Thanks for the help.


Jeff Johnson said:
I have a C# applications that makes use of user settings using the
autobuilt settings class. Using the IDE, I created a couple of user
settings such as Key1=Value1, Key2=Value2 and so on. In my program I have
a variable that holds the name of the user setting I want to read. How do
I use the variable to access the user setting?

The code I have is something like this
string myVar = "Key2";
I want to do something like
string result = MyApp.Properties.Settings.myVar;

Obviously the above line doesn't work because myVar is a variable. How do
I work around this?

A) I think you'd really be wanting

MyApp.Properties.Settings.Default.myVar;

B) Just off the top of my head, does the class returned by the Default
property have an indexer? If so, try:

MyApp.Properties.Settings.Default[myVar];

and see what you get!
 
J

Jeff Johnson

Thanks for your reply.
-Option A doesn't work. It doesn't see myVar as a member of
"MyApp.Properties.Settings.Default". This is the problem: myVar is not a
member. The value of myVar is however.

I was simply saying that you had left ".Default" out of your example. This
is why you should never type code into a post by hand but rather copy and
paste actual code.
-Option B does work. Excellent. The Default property does have an indexer
and hence
MyApp.Properties.Settings.Default[myVar];
works perfectly.

Strangely, the reason I didn't go down this path is because I never got
".Default" when using IntelliSense as an option.
I created a member in my main form of type "MyApp.Properties.Settings" and
then used
mySettings = new MyApp.Properties.Settings();
after the form initialization for easy reference to the settings.
The thing is that when I then use the mySettings reference it does not
bring me ".Default" as member of the class. However I now discovered that
there is an indexer on MyApp.Propertoes.Settings itself.

That's because Default is a static property and can only be accessed through
the class, not an instance. I question why you created a new instance of it
in the first place. I would have just done

MyApp.Properties.Settings mySettings = MyApp.Properties.Settings.Default;
 
N

NJD

Jeff, your response is perfect! I hadn't realized that "Default" was a
static property and your suggest to simply use a reference rather than
create a new instance is what I really what I wanted. I'm new to OOP and C#,
but am beginning to love it.

Jeff Johnson said:
Thanks for your reply.
-Option A doesn't work. It doesn't see myVar as a member of
"MyApp.Properties.Settings.Default". This is the problem: myVar is not a
member. The value of myVar is however.

I was simply saying that you had left ".Default" out of your example. This
is why you should never type code into a post by hand but rather copy and
paste actual code.
-Option B does work. Excellent. The Default property does have an indexer
and hence
MyApp.Properties.Settings.Default[myVar];
works perfectly.

Strangely, the reason I didn't go down this path is because I never got
".Default" when using IntelliSense as an option.
I created a member in my main form of type "MyApp.Properties.Settings"
and then used
mySettings = new MyApp.Properties.Settings();
after the form initialization for easy reference to the settings.
The thing is that when I then use the mySettings reference it does not
bring me ".Default" as member of the class. However I now discovered that
there is an indexer on MyApp.Propertoes.Settings itself.

That's because Default is a static property and can only be accessed
through the class, not an instance. I question why you created a new
instance of it in the first place. I would have just done

MyApp.Properties.Settings mySettings = MyApp.Properties.Settings.Default;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top