[英]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".我有一个设置“Properties.Settings.Default.Password1”和“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.如果您使用的是 C# 3.0 或更高版本,则var
是一个不错的选择。
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.否则,hover 在开发环境中初始化语句(例如Password1
)的右侧。 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
.) (题外话建议:按照微软的 C# 和 .NET 代码样式指南的建议, passwordPath
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 - 这使编译器为您决定实际类型(在编译时,因此 IntelliSense 等仍然可以利用 static 类型)。
var Passwordpath = Properties.Settings.Default.Password1
I'm not really sure what are you trying to do though.不过,我不确定您要做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.