Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8455435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:17:16+00:00 2026-06-10T12:17:16+00:00

So, I have a dictionary called Potions. I need to check if a key

  • 0

So, I have a dictionary called Potions. I need to check if a key in potions has the same name as an object passed as an argument. Now I’m able to do that but I can’t figure out how to add a value to that particular key if the item object and the key have the same name. This code works fine for the first instance of an object. But when I add another instance object with the same name as a key, I get a key not found exception. I understand that the 2 objects wont be the same.How can I extract the object reference inside the dictionary? Or is there another way?

public static void addItem(Potion item)
 {
    if (Potions.Count >0)
    {
        foreach(KeyValuePair<Potion,int> pair in Potions)
        {
            if (pair.Key.itemName == item.itemName)
            {
            containsItem = true;
            }   
        }
        if (containsItem)
        {
        Potions[item] += 1;
        Debug.Log (Potions[item]);
        containsItem = false;
        }
        else
            {       
            Potions.Add(item,1);
            }
    }
    else
    {
    Potions.Add (item,1);
    }

    foreach(KeyValuePair<Potion,int> pair in Potions)
    {
        Debug.Log (pair.Key.itemName + " : " + pair.Value);
    }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:17:18+00:00Added an answer on June 10, 2026 at 12:17 pm

    You can override Equals and GetHashCode, but that might have other implications. Instead, you can use an IEqualityComparer when you create the dictionary, like so:

    class Potion {
        public string Name;
        public int Color;
    }
    
    class PotionNameEqualityComparer : IEqualityComparer<Potion> {
        public bool Equals(Potion p1, Potion p2) {
            return p1.Name.Equals(p2.Name);
        }
        public int GetHashCode(Potion p1) {
            return p1.Name.GetHashCode();
        }
    }
    
    void Main() {
        var d = new Dictionary<Potion, int>(new PotionNameEqualityComparer());
        var p1 = new Potion() { Name = "Health", Color = 1 };
        var p2 = new Potion() { Name = "Health", Color = 2 };
        d.Add(p1, 1);
        d[p2]++; // works, and now you have two health potions. 
                 // Of course, the actual instance in the dictionary is p1; 
                 // p2 is not stored in the dictionary.    
    }           
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary (see below) that has Key strings and Value strings and
I have a dictionary with a key called ev#### where #### is some number
I have a simple but huge table called 'dictionary' which has 2 columns. Id
I have a key/value pairs mapped by hash (ID) in Dictionary<string, Dictionary<string, string>> called
I have an interface called Dictionary which has a method insert() . This interface
I need some help here. I have a mysql table called dictionary like this:
I have a list with dictionary as elements. Each dictionary has an entry called
I have a dictionary table called car_status that has an 'id' column and a
I have a dictionary object called obj_1 and it's values are shown like this:
Say I have a Dictionary object which contains many instances of an object called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.