简体   繁体   中英

Creating a classification system using C# Dictionary

I'll try to give you an idea of what I am trying to do.

Let's say I have 4 columns in my datagridview/table:

+----+-------------+------------+------------+
| ID | Description |    Type    |   Class    |
+----+-------------+------------+------------+
|  1 | Soft        | Strawberry | Fruit      |
|  2 | Round       | Grape      | Fruit      |
|  3 | Sharp       | Pencil     | Stationary |
+----+-------------+------------+------------+

This is what the final form should look like after the program is through.

Let's assume the second and last row are empty and the third row, Type, is the only one available.

I had originally attempted something like this with a switch code block like so:

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

Basically, we take type as the criteria argument for the other two cells. Strawberry is soft and fruit, Grape is round and fruit, etc.

I was advised to use a dictionary instead of a switch block because of the lengthy criteria's (maybe 1000 types) however, as far as I know while a dictionary can be called using a key (like Strawberry, or Grape) it will only be capable of returning one type, say, the data of Description. Moreover, I do not know how I will be able to handle writing the dictionary into datagridview format, compared to the ease of writing it with a switch statement.


Solution I came up with for future viewers:

    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"}}
            };
     }

Active control:

    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;
            }
        }
    }

No , do not use a switch statement.

The value part of the dictionary entry can be an object of any type, not just something simple like String. So populate your value with a class of any complexity you wish.

// 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);

Edited to add:

Iterating through the Dictionary (code taken from 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
    */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM