過去的問題線程,並鏈接到fullpast代碼HERE在TryGetValue字典中,如果它是類的一部分,我該如何編寫out參數(並獲取它的值)?
創建我有一個類字典參數,因此它可以容納兩個string
值。現在我想要寫的字符串TryGetValue
到out
都在這個類:
public class DictionaryInitializer
{
public class DictionarySetup
{
public string theDescription { get; set; }
public string theClass { get; set; }
}
正如你所看到的,有嵌套在DictionarySetup
theDescription
和theClass
。然後我會在這裏使用該類創建字典:
public class DictionaryInit
{
//IS_Revenues data
public Dictionary<int, DictionarySetup> accountRevenue = new Dictionary<int, DictionarySetup>()
{
{ 400000, new DictionarySetup {theDescription="Call", theClass="Revenues"}}
};
public Dictionary<int, DictionarySetup> accountExpenses = new Dictionary<int, DictionarySetup>()
{
{790100, new DictionarySetup { theDescription="Currency Hedge", theClass="Other income/expense"}}
};
}
然後,在我計劃在字典用我TryGetValue
我的擴展方法:
public void DictionaryUseKey(int MapCode, int MapKey, int rowindex, Dictionary<int, DictionarySetup> AccountLexicon)
{
AccountLexicon[1] = new DictionarySetup();
DictionarySetup Classes;
DictionarySetup Descriptions;
//Saw the above code in another thread, not sure if it's what I should be doing but it seems pretty close to what I want, however, I don't know how to specify the DictionarySetup.theDescription for example;
AccountLexicon.TryGetValue(MapKey, out Classes);
{
//I want to be able to write theDescription and theClass into string variables for use below if the `TryGetValue` returns true, but it seems to me that it can only out one value? How does this work?
DGVMain.Rows[rowindex].Cells[3].Value = ?? how do I write something like... theValues.theDescription;
DGVMain.Rows[rowindex].Cells[11].Value = ?? how do I write something like... theValues.theClass;
}
}
最後,我呼籲在擴展方法我事件像這樣:
private void btnMapper_Click(object sender, EventArgs e)
{
for (int rowindex = 0; rowindex < DGVMain.RowCount; rowindex++)
{
int accountKey = Convert.ToInt32(DGVMain.Rows[rowindex].Cells[2].Value);
int projCode = Math.Abs(Convert.ToInt32(DGVMain.Rows[rowindex].Cells[7].Value));
int deptCode = Math.Abs(Convert.ToInt32(DGVMain.Rows[rowindex].Cells[9].Value));
int AbsoluteKey = Math.Abs(accountKey);
while (AbsoluteKey >= 10) { AbsoluteKey /= 10; }
while (deptCode >= 10) { deptCode /= 10; }
theDictionary = new DictionaryInit();
DictionaryUseKey(deptCode, accountKey, theDictionary.accountRevenue);
}
}
我沒有在您的問題中看到任何用戶定義的擴展方法。 – Amy
您只能使用帶有超出參數的變量(以及有關該參數的參數)。從你的話來看,它看起來像你試圖使用一個屬性。 –
@Amy對不起,我不知道這些叫做什麼。功能?只是方法?術語擴展方法是用像我上面寫的那樣的例子拋出的:DictionaryUseKey(deptCode,accountKey,theDictionary.accountRevenue); – Arvayne