【问题标题】:Creating a classification system using C# Dictionary使用 C# Dictionary 创建分类系统
【发布时间】:2017-03-20 04:44:10
【问题描述】:

我会尽量让你知道我想要做什么。

假设我的 datagridview/table 中有 4 列:

+----+-------------+------------+------------+ |身份证 |说明 |类型 |班级 | +----+-------------+------------+------------+ | 1 |软 |草莓 |水果 | | 2 |圆形 |葡萄 |水果 | | 3 |夏普 |铅笔 |文具 | +----+-------------+------------+------------+

这是程序完成后的最终形式。

假设第二行和最后一行是空的,第三行 Type 是唯一可用的。

我最初尝试使用这样的开关代码块进行类似的操作:

switch (Type) 
{    
    (Strawberry){
     B2 = Soft;
     C3 = Fruit;
    }
    (Grape){    
     etc...
    }    
}

基本上,我们将类型作为其他两个单元格的标准参数。 草莓软有果味,葡萄圆润有果味等。

我被建议使用字典而不是 switch 块,因为标准很长(可能有 1000 种类型),但是据我所知,虽然可以使用键(如草莓或葡萄)调用字典,但它会只能返回一种类型,比如 Description 的数据。此外,与使用 switch 语句编写字典相比,我不知道如何将字典写入 datagridview 格式。


我为未来的观众提出的解决方案:

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

    public class DictionaryInit
    {
        public Dictionary<int, DictionarySetup> accountRevenue = new Dictionary<int, DictionarySetup>()
            {
                { 400000, new DictionarySetup {theDescription="Black", theClass="Weapon"}},
                { 400001, new DictionarySetup {theDescription="White", theClass="Mouse"}},
                { 410000, new DictionarySetup {theDescription="Opaque", theClass="Obstacle"}}
            };
     }

主动控制:

    DictionaryInit theDictionary;
    private void btnFixer_Click(object sender, EventArgs e)
    {
        theDictionary = new DictionaryInit();
        for (int rowindex = 0; rowindex < DGVMain.RowCount; rowindex++)
        {
            foreach (var item in theDictionary.accountRevenue)
            {
                int theKey = item.Key;
                DictionarySetup theValues = item.Value;
                DGVMain.Rows[rowindex].Cells[5].Value = theValues.theDescription;
                DGVMain.Rows[rowindex].Cells[6].Value = theValues.theClass;
            }
        }
    }

【问题讨论】:

    标签: c# arrays dictionary datagridview switch-statement


    【解决方案1】:

    ,不要使用 switch 语句。

    字典条目的value 部分可以是任何类型的对象,而不仅仅是像字符串这样的简单对象。所以用你想要的任何复杂度的类来填充你的value

    // NOT TESTED
    
    class thing
    {
    public:
        string theType;
        string theClass;
    };
    
    Dictionary<string, thing> dict = new Dictionary<string, thing>();
    
    thing x;
    x.theType = "Soft";
    x.theClass = "Fruit";
    dict.Add("Strawberry", x);
    

    编辑添加:

    遍历字典(代码取自here

    foreach(var item in myDictionary)
    {
        string theKey = item.Key;
        thing theValues = item.Value;
    
        /* 
            theKey, theValues.theClass, and theValues.theType
            have the values you want for your datagrid
        */
    }
    

    【讨论】:

    • 数据网格是我假设/获取 [Type] 标准以确定 [Description] 和 [Class] 值的地方。所以我最初的计划是制定一个开关/字典,其中包含各种 [Type] 以及检测到某种类型时要执行的操作。操作是写入数据网格的空列。
    • 基本上,我认为我会用字典做的是.. 遍历列 [Type] 的行,然后执行 if Dictionary.ContainsKey(TypeCellDataisKEY) 并返回任何与 Key 和将它们分配到侧面的空单元格中。老实说,我根本不知道我是否应该使用字典,或者可能是列表或其他。
    • 必须将你的类中的字符串公开,以便它通过编译器工作。明天我会用一些代码来测试它。
    • 太棒了!它有效,感谢您引导我完成此操作。我将在 OP 中发布我想出的代码。
    猜你喜欢
    • 2023-02-02
    • 2010-11-04
    • 2023-04-04
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2022-01-06
    • 2023-03-10
    • 2021-04-20
    相关资源
    最近更新 更多