C#在扩展类中调用字典?

时间:2017-03-27 07:59:39

标签: c# dictionary extension-methods

完全按照我的程序重写所有内容:

class DictionaryInitializer
{
    public class DictionarySetup
    {
        public string theDescription { get; set; }
        public string theClass { get; set; }
    }
    public class DictionaryInit
    {
        //IS_Revenues data
        public Dictionary<int, DictionarySetup> accountRevenue = new Dictionary<int, DictionarySetup>()
            {
                { 400000, new DictionarySetup {theDescription="Call", theClass="Revenues"}},
                { 400001, new DictionarySetup {theDescription="Bill", theClass="Revenues"}},
                { 495003, new DictionarySetup {theDescription="Revenue", theClass="Revenues"}}
            };


        public Dictionary<int, DictionarySetup> accountExpenses = new Dictionary<int, DictionarySetup>()
            {
                {790130, new DictionarySetup { theDescription="Currency Hedge", theClass="Other income/expense"}},
                {805520, new DictionarySetup { theDescription="Int Income", theClass="Other income/expense"}}
            };
}

在我的主要形式上:

    DictionaryInit theDictionary;
    btnClick() {

    theDictionary = new DictionaryInit();
    //Some code to loop through a datagridview        
    //Somemore Code
                    foreach (var item in theDictionary.accountRevenue)
                    {
                        int theKey = item.Key;
                        if (theKey == keyCode)
                        {
                            DictionarySetup theValues = item.Value;
                            DGVMain.Rows[rowindex].Cells[3].Value = theValues.theDescription;
                            DGVMain.Rows[rowindex].Cells[11].Value = theValues.theClass;
                            DGVMain.Rows[rowindex].Cells[12].Value = "Sale of Services";
                            Recording(rowindex);
                        }
                    }
    }

当前正在进行的工作:

                    DictionarySetup theValue;
                    if (theDictionary.accountExpenses.TryGetValue(keyCode,out theValue.theDescription) //[5]-> Account Type
                    {
                      //Some code to write dictionary data to the data grid view.

我正在努力使TryGetValue和Contains(值)字典功能现在正常工作。

我当前的错误消息如下:

尝试trygetvalue时,

“属性或索引器可能不会作为out或ref参数传递”

最后在尝试扩展方法时,我正在尝试创建:

 "Inconsistent accessibility, Dictionary<int, DictionaryInitializer.DictionarySetup> is less accessible than the method DictionaryUse<int, DictionaryInitializer.DictionarySetup>"

1 个答案:

答案 0 :(得分:1)

你必须让你的领域公开....

Dictionary<int, DictionarySetup> accountRevenue

应该是

public Dictionary<int, DictionarySetup> accountRevenue

如果你想从课外引用它..

这部分似乎也缺少变量名称;

public void DictionaryUse (int code, int key, Dictionary)

应该是

public void DictionaryUse (int code, int key, Dictionary<int, DictionarySetup> theDictionary)

但我同意其他评论,你好像是在重新发明轮子,只是使用现有的Dictionary实用程序方法