[英]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.