[英]What type of variable Properties.Settings.Default c# wpf
我有一个设置“Properties.Settings.Default.Password1”和“Properties.Settings.Default.Password2”的路径。
现在我想使用这些路径之一。 我使用以下代码:
If (certain condition)
{
kindOfVariable passwordPath = Properties.Settings.Default.Password1
}
else
{
kindOfVariable passwordPath = Properties.Settings.Default.Password2
}
好吧,我知道密码是一个字符串,没问题,但我想要路径。
但是我必须使用什么样的变量? 还是有其他方法可以做到这一点?
通常你会像这样保存一个新值:
Properties.Settings.Default.passwordPath = "New Password";
Properties.Settings.Default.Save();
我想对路径做的是在该路径上赋予新值,例如
passwordPath = "New Password";
Properties.Settings.Default.Save();
如果您使用的是 C# 3.0 或更高版本,则var
是一个不错的选择。
这会导致编译器从其初始化语句右侧的表达式中自动推断局部变量的类型。
if (certain condition)
{
var Passwordpath = Properties.Settings.Default.Password1
}
else
{
var Passwordpath = Properties.Settings.Default.Password2
}
否则,hover 在开发环境中初始化语句(例如Password1
)的右侧。 您应该会看到一个提供其类型的工具提示。 使用那个。
(题外话建议:按照微软的 C# 和 .NET 代码样式指南的建议, passwordPath
Passwordpath
)
编辑以回答更新的问题:
最简单的方法就是颠倒逻辑。 与其尝试存储属性的地址并稍后将其设置为新值,不如将新值存储在临时变量中,并使用它直接设置属性。 也许用一些代码来解释会更容易......
// 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();
您始终可以使用 var - 这使编译器为您决定实际类型(在编译时,因此 IntelliSense 等仍然可以利用 static 类型)。
var Passwordpath = Properties.Settings.Default.Password1
不过,我不确定您要做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.