CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2008
    Posts
    154

    [RESOLVED] Dictionary Generic Class, Key.Value = count # of Class

    I don't know if this is the way to go

    I have Dictionary<Class, int>
    the int value - counts how many of that Class is stored there
    I have a method that does that

    Code:
            public void DictionaryAddClass(Class Class)
            {
                if (Dictionary.ContainsKey(Class))
                {
                    int value = Dictionary[Class];
                    value++;
                    Dictionary[Class] = value;
                }
                else
                {
                    Dictionary.Add(Class, 1);
                }
            }
    so if we did this
    Code:
                Class myclass = new Class ("AClass");
                DictionaryAddClass(myclass );
                DictionaryAddClass(myclass );
                DictionaryAddClass(myclass );
    the value of that Key would be 3

    Here is the probliem - I learned that if somewhere else I create a new class
    Class myclass = new Class ("AClass");
    even with the same parameters it treats it as a new Key
    and this > if (Dictionary.ContainsKey(Class))
    will not be true

    I need to hold a number of these Classes in some kind of Inventory and store how many I have. Should I be using Dictionary or doing something else?

  2. #2
    Join Date
    Jun 2008
    Posts
    154

    Re: Dictionary Generic Class, Key.Value = count # of Class

    I have an idea...

    Maybe I should hold the name of the Property of Class in there instead

    Dictionary<string, int>

    Class myclass = new Class ("AClass");
    string name = myclass.Name

    DictionaryAddClass(name);
    DictionaryAddClass(name);
    DictionaryAddClass(name);

    then if somewhere else I instance another Class and name it "AClass"

    if (Dictionary.ContainsKey(name)) this will actually be true.

    I will think on this...

  3. #3
    Join Date
    Jun 2008
    Posts
    154

    Re: Dictionary Generic Class, Key.Value = count # of Class

    Solved! This worked soooo much better than actually storing the Class as a Key. I at first though I should store the class there if I need to access it properties. But since I can simply match the a string with the class Property.Name I can easily access the class properties that way. Much faster and easier than the other way.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: [RESOLVED] Dictionary Generic Class, Key.Value = count # of Class

    You could also do it by overriding GetHashCode and Equals on your class. These are the methods the dictionary uses to check if two classes are supposed to be the same.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: [RESOLVED] Dictionary Generic Class, Key.Value = count # of Class

    What about using Type instead of Class?
    Code:
    public void DictionaryAddClass(Type t)
    {
       ...
    }
    ...
    Class myclass = new Class ("AClass");\
    DictionaryAddClass(myclass.GetType());

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured