在TryGetValue字典中,如果它是类的一部分,我该如何编写out参数(并获取它的值)?

问题描述:

过去的问题线程,并链接到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

其实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