开发者

What type of variable Properties.Settings.Default c# wpf

I have a path to an setting "Properties.Settings.Default.Password1" and "Properties.Settings.Default.Password2".

Now I want to use one of these paths. I use the following code:

If (certain condition)
{
kindOfVariable passwordPath = Properties.Settings.Default.Password1
}
else
{
kindOfVariable passwordPath = Properties.Settings.Default.Password2
}

Well, I know that the password is a string, thats no problem but I want the path.

But what kind of variable do I have to use? Or is there another way to do this?

Normally you would save a new value like this:

Properties.Settings.Default.passwordPath = "New Password";
Properties.Settings.Default.Save();

What I wanna do with the path is to give a new value on that path, so for example

passwordPath = "New Password";
Properties.Settings.Default.Save()开发者_如何转开发;


If you're using C# 3.0 or later, var is a good choice.

That causes the compiler to automatically infer the type of a local variable from the expression on the right side of its initialization statement.

if (certain condition)
{
   var Passwordpath = Properties.Settings.Default.Password1
}
else
{
   var Passwordpath = Properties.Settings.Default.Password2
}

Otherwise, hover over the right side of the initialization statement (Password1, for example) in the development environment. You should see a tooltip that gives away its type. Use that one.


(Off-topic suggestion: Name your local variables using camelCasing, as recommended by Microsoft's C# and .NET code style guidelines. The Passwordpath variable should really be passwordPath.)


Edit to answer updated question:

The simplest way is just to reverse the logic. Rather than trying to store the address of the property and set it to a new value later, just store the new value in a temporary variable, and use it to set the property directly. Maybe it would be easier to explain with some code...

// Get the new password
string password = "New Password";

// Set the appropriate property
if (certain condition)
{
   Properties.Settings.Default.Password1 = password;
}
else
{
   Properties.Settings.Default.Password2 = password;
}

// Save the new password
Properties.Settings.Default.Save();


You could always use var - that makes the compiler decide the actual type for you (at compile time, so IntelliSense etc. can still take advantage of static typing).

var Passwordpath = Properties.Settings.Default.Password1

I'm not really sure what are you trying to do though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜