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

    [RESOLVED] Dictionary Key Subclass get Property

    with some suggestions from BigEd, I've decided to start learning Dictionarys..

    but I got a small problem,
    I am storing Items into a Dictionary Inventory
    Item is class that has a bunch of properties, one of them is Name.

    I am adding Items to the inventory like this
    Code:
            public void InventoryAddItem(Item Item)
            {
                if (invItems.ContainsKey(Item))
                {
                    int value = invItems[Item];
                    value++;
                    invItems[Item] = value;
                }
                else
                {
                    invItems.Add(Item, 1);
                }
            }
    that way I can use the value of the Key to determine how many of that Item I am storing.

    But now I need to create a string array and post it in a GUI to show what items and how many there are in the Inventory

    Code:
                foreach (KeyValuePair<Item, int> item in GameRef.AdventureParty.InvItems)
                {
                    string itemName = item.Name
                    ItemNamesToAdd.Add(itemName);
                }
    that's the idea.. but the code doesn't work - how do I access the Key as subclass properties?

  2. #2
    Join Date
    Jun 2008
    Posts
    154

    Re: [RESOLVED] Dictionary Key Subclass get Property

    ok figured it out - obviously I need more coffee

    Code:
                foreach (KeyValuePair<Item, int> item in GameRef.AdventureParty.InvItems)
                {
                    string itemName = item.Key.Name;
                    ItemNamesToAdd.Add(itemName);
                }

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