C#在扩展类中调用字典吗? [英] C# Calling a dictionary within an extension class?

查看:304
本文介绍了C#在扩展类中调用字典吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完全按照程序的要求重新编写所有内容:

Re-written everything exactly as my program has it:

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(value)字典函数现在可以正常工作.

I'm working on making the TryGetValue and Contains(value) dictionary functions to work for now.

我当前的错误消息如下:

My current error messages are as follows:

尝试trygetvalue时,属性或索引器可能不会作为out或ref参数传递"

"a property or indexer may not be passed as an out or ref parameter" when attempting the trygetvalue

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

and finally when trying the extension method i'm trying to create:

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

推荐答案

您必须公开您的字段....

You have to make your field public....

Dictionary<int, DictionarySetup> accountRevenue

应该是

public Dictionary<int, DictionarySetup> accountRevenue

如果要从班级外部引用它..

if you want to refer to it from outside the class..

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

This part seems to also be missing a variable name;

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

应该是

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

但是我同意其他评论,您似乎正在重新发明轮子,仅使用现有的Dictionary实用程序方法

But I agree with the other comments, you seem to be re-inventing the wheel, just use the existing Dictionary utility methods

这篇关于C#在扩展类中调用字典吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆