0

过去的问题线程和完整过去代码的链接在这里

我用一个参数类创建我的字典,以便它可以保存两个string值。现在我正在尝试将 a 写入TryGetValue此类out中的两个字符串:

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

如您所见,有theDescriptiontheClass嵌套在DictionarySetup. 然后我会在这里使用该类创建字典:

    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);
        }
    }
4

1 回答 1

2

实际上该TryGetValue方法会返回一个布尔值,表示指定键的存在,如果找到该键,对应的值将存储在 out 参数中。在您的情况下, out 参数是Classes并且在您的代码中定义如下DictionarySetup Classes:这意味着如果字典中存在键,则意味着相应的DictionarySetup对象将存储在中, Classes因此您可以访问theDescriptionand theClassfrom Classes;考虑下面的代码:

 if(AccountLexicon.TryGetValue(MapKey, out Classes))
 {
    DGVMain.Rows[rowindex].Cells[3].Value =  Classes.theDescription;
    DGVMain.Rows[rowindex].Cells[11].Value = Classes.theClass;
 }
于 2017-03-28T05:44:16.990 回答