簡體   English   中英

C#在擴展類中調用字典?

[英]C# Calling a dictionary within an extension class?

完全按照程序的要求重新編寫所有內容:

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)字典函數現在可以正常工作。

我當前的錯誤消息如下:

嘗試trygetvalue時“屬性或索引器可能不作為out或ref參數傳遞”

最后,當嘗試使用擴展方法時,我嘗試創建:

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

您必須公開您的領域...。

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實用程序方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM