C# 在扩展 class 中调用字典?
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) 字典函数暂时可用。
我目前的报错信息如下:
"a property or indexer may not be passed as an out or ref parameter" 尝试 trygetvalue
最后在尝试我尝试创建的扩展方法时:
"Inconsistent accessibility, Dictionary<int, DictionaryInitializer.DictionarySetup> is less accessible than the method DictionaryUse<int, DictionaryInitializer.DictionarySetup>"
你必须让你的领域public.....
Dictionary<int, DictionarySetup> accountRevenue
应该是
public Dictionary<int, DictionarySetup> accountRevenue
如果你想从 class..
之外引用它
这部分好像还少了一个变量名;
public void DictionaryUse (int code, int key, Dictionary)
应该是
public void DictionaryUse (int code, int key, Dictionary<int, DictionarySetup> theDictionary)
但我同意其他评论,你似乎在重新发明轮子,只需使用现有的 Dictionary 实用方法
完全按照我的程序重写所有内容:
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) 字典函数暂时可用。
我目前的报错信息如下:
"a property or indexer may not be passed as an out or ref parameter" 尝试 trygetvalue
最后在尝试我尝试创建的扩展方法时:
"Inconsistent accessibility, Dictionary<int, DictionaryInitializer.DictionarySetup> is less accessible than the method DictionaryUse<int, DictionaryInitializer.DictionarySetup>"
你必须让你的领域public.....
Dictionary<int, DictionarySetup> accountRevenue
应该是
public Dictionary<int, DictionarySetup> accountRevenue
如果你想从 class..
之外引用它这部分好像还少了一个变量名;
public void DictionaryUse (int code, int key, Dictionary)
应该是
public void DictionaryUse (int code, int key, Dictionary<int, DictionarySetup> theDictionary)
但我同意其他评论,你似乎在重新发明轮子,只需使用现有的 Dictionary 实用方法