2017-03-28 54 views
0

過去的問題線程,並鏈接到fullpast代碼HERE在TryGetValue字典中,如果它是類的一部分,我該如何編寫out參數(並獲取它的值)?

創建我有一個類字典參數,因此它可以容納兩個string值。現在我想要寫的字符串TryGetValueout都在這個類:

public class DictionaryInitializer 
{ 
    public class DictionarySetup 
    { 
     public string theDescription { get; set; } 
     public string theClass { get; set; } 
    } 

正如你所看到的,有嵌套在DictionarySetuptheDescriptiontheClass。然後我會在這裏使用該類創建字典:

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); 
     } 
    } 
+1

我沒有在您的問題中看到任何用戶定義的擴展方法。 – Amy

+1

您只能使用帶有超出參數的變量(以及有關該參數的參數)。從你的話來看,它看起來像你試圖使用一個屬性。 –

+2

@Amy對不起,我不知道這些叫做什麼。功能?只是方法?術語擴展方法是用像我上面寫的那樣的例子拋出的:DictionaryUseKey(deptCode,accountKey,theDictionary.accountRevenue); – Arvayne

回答

2

其實TryGetValue方法返回一個布爾值表示指定鍵的存在,如果鍵被找到時,科爾esponding的值將被存儲在out參數中。在你的情況下,out參數是Classes,在你的代碼中定義如下:DictionarySetup Classes。這意味着如果鑰匙出現在字典中意味着相應的DictionarySetup對象將被存儲在Classes中,因此您可以從Classes訪問theDescriptiontheClass;考慮下面的代碼:

if(AccountLexicon.TryGetValue(MapKey, out Classes)) 
{ 
    DGVMain.Rows[rowindex].Cells[3].Value = Classes.theDescription; 
    DGVMain.Rows[rowindex].Cells[11].Value = Classes.theClass; 
} 
+1

啊,所以我似乎在正確的軌道上。讓我測試你的建議。 – Arvayne

相關問題