Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,
I made a Dictionary of user defined function Person. I want to check the data from dictionary that it is present in the dictionary or not i am using the dictionary function contains value to check whether it is present in dictionary or not.
but it is not searching from it.

What I have tried:

public bool SearchPerson(Dictionary<int, Person> dict,Person pr)
       {


               if (dict.ContainsValue(pr)== true)
               {
                   Console.WriteLine("Successfully found");

                   return true;
               }

               return false;

       }


Main Function

Person nam = new Person();

                   nam.setName(name);

                   nam.SearchPerson(dictionary,nam);



Set and get methods of Person class
public void setName(string name)
        {
            Console.WriteLine("Please Enter the name of the Person");
            name = Console.ReadLine();
            this.name = name;

        }
        public string getName(Person p)
        {
            return this.name;
        }
Posted
Updated 24-Oct-19 0:11am
Comments
Chris Copeland 24-Oct-19 5:59am    
I can't see, in the code you provided, where you're actually adding to the person object to the dictionary?
Richard MacCutchan 24-Oct-19 6:00am    
Where is the code that inserts the entries to the dictionary?

The reason it doesn't work for you is that you don't add nam to your dictionary, and since you compare instances, it has to be the exact instance, not just one with the same name property.
Add it, and it'll work:
C#
Person nam = new Person();
nam.setName(name);
dictionary.Add(nam);
nam.SearchPerson(dictionary,nam);
But ... That's pretty confused code.
Either SearchPerson should be static and you call it via the class name:
C#
if (Person.SearchPerson(dictionary, nam))
   {
   ...
Or you search for the current item rather than passing an instance through:
C#
public bool SearchPerson(Dictionary<int, Person> dict)
    {
    if (dict.ContainsValue(this))
        {
        Console.WriteLine("Successfully found");
        return true;
        }
    return false;
    }
But I'd probably not make that method part of the Person class at all, since it relies on structures outside the class - the Dictionary<int, Person>
 
Share this answer
 
You should define the value equality for the Person class. See How to: define value equality for a type - C# Programming Guide | Microsoft Docs[^].
 
Share this answer
 
Some of your code doesn't much much sense, but for this code I have updated your setName

public void setName(string name)
{
    Console.WriteLine("Please Enter the name of the Person");
    this.name = name;
}


There is no point passing in a "name" for it to just be overriden with a console input.

Your dictionary isn't finding your object because it is searching for the actual object you are giving it

C#
Dictionary<int, Person> dictionary = new Dictionary<int, Person>();

Person personJohn = new Person();
personJohn.setName("John");
dictionary.Add(1, personJohn);

Person personDave = new Person();
personDave.setName("Dave");
dictionary.Add(2, personDave);


Person nam = new Person();
bool success = nam.SearchPerson(dictionary, personJohn);

// success is true because we are looking for an object (personJohn) that is in the dictionary

Person tempPerson = new Person();
tempPerson.name = "John";

success = nam.SearchPerson(dictionary, tempPerson);

// success is false because "tempPerson" isn't in the dictionary
// even though it has the same "name" as an object in the dictionary
// the code doesn't know that constitutes a match


You don't want your code to work at the object level though, you are only interested in matching property values, not matching objects. You can update your search function like so

C#
public bool SearchPerson(Dictionary<int, Person> dict, Person pr)
{

    if (dict.Values.Any(p => p.name == pr.name))
    {
        Console.WriteLine("Successfully found");

        return true;
    }

    return false;

}


Another solution is to create a custom comparer that tells the code that two objects are equivalent if the name properties are the same.

public class PersonComparer : IEqualityComparer<Person>
{
    public bool Equals(Person x, Person y)
    {
        if (x == null || x.name == null || y == null || y.name == null)
        {
            return false;
        }

        return x.name.Equals(y.name);
    }

    public int GetHashCode(Person obj)
    {
        return obj.name.GetHashCode();
    }
}


Your search function will now look like

public bool SearchPerson(Dictionary<int, Person> dict, Person pr)
{

    if (dict.Values.Contains(pr, new PersonComparer()))
    {
        Console.WriteLine("Successfully found");

        return true;
    }

    return false;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900