Checking the equality of objects in C#

Started by
2 comments, last by Enerjak 9 years, 3 months ago

I don't think the title explains my problem quite well, but.... What I'm having problems with is this:

I have a class named Person.cs:


class Person : Object
    {
        protected Name m_personName;
        protected string m_state;
        protected string m_zipCode;
        protected string m_PhoneNumber;
        protected string m_address;
        protected string m_age;
        public Person()
        {
            m_personName = new Name();
            m_state = "New Jersey";
            m_zipCode = "08731";
            m_PhoneNumber = "(609)-255-1122";
            m_address = "316 Bridge Drive";
            m_age = "30";
        }

        public Person(string firstName,
            string lastName,
            string state,
            string zipCode,
            string phoneNumber,
            string address,
            string age)
        {
            m_personName = new Name(firstName, lastName);
            m_state = state;
            m_zipCode = zipCode;
            m_PhoneNumber = phoneNumber;
            m_address = address;
            m_age = age;
        }

        public void setName(string firstName, string lastName)
        {
            m_personName.setFirstName(firstName);
            m_personName.setLastName(lastName);
        }

        public void setName(Name name)
        {
            m_personName = name;
        }

        public Name getName()
        {
            return m_personName;
        }

        public void setAddress(string address)
        {
            m_address = address;
        }

        public string getAddress()
        {
            return m_address;
        }

        public void setPhoneNumber(string phoneNumber)
        {
            m_PhoneNumber = phoneNumber;
        }

        public string getPhoneNumber()
        {
            return m_PhoneNumber;
        }

        public void setState(string state)
        {
            m_state = state;
        }

        public string getState()
        {
            return m_state;
        }

        public void setZipCode(string ZipCode)
        {
            m_zipCode = ZipCode;
        }

        public string getZipCode()
        {
            return m_zipCode;
        }

        public void setAge(string age)
        {
            m_age = age;
        }

        public string getAge()
        {
            return m_age;
        }
        public bool CompareTo(Person p)
        {
            if(this.getName() == p.getName() &&
               this.getAddress() == p.getAddress() &&
               this.getAge() == p.getAge() &&
                this.getState() == p.getState() &&
                this.getPhoneNumber() == p.getPhoneNumber() &&
                this.getZipCode() == p.getZipCode())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

I also have a class named - fittingly - Name.cs


 class Name
    {
        protected string FirstName;
        protected string LastName;

        public Name()
        {
            FirstName = "";
            LastName = "";
        }

        public Name(string firstName, string lastName)
        {
            FirstName = firstName;
            LastName = lastName;
        }
        public void setFirstName(string fName)
        {
            FirstName = fName;
        }

        public String getFirstName()
        {
            return FirstName;
        }

        public void setLastName(string lName)
        {
            LastName = lName;
        }

        public string getLastName()
        {
            return LastName;
        }

    }

Now I have a List<Person> variable used to add a new person to the array that is List<Person>, the entry is added to the array, that's not the problem, the problem is searching the array for a specific value:


     String firstName = TBFirstNameSearch.Text;
            String lastName = TBLastNameSearch.Text;
            String Age = TBAgeSearch.Text;
            String Address = TBAddressSearch.Text;
            String State = TBStateSearch.Text;
            String PhoneNumber = TBPhoneNumberSearch.Text;
            String ZipCode = TBZipCodeSearch.Text;
            Person pSearch = new Person(firstName, lastName, State, ZipCode, PhoneNumber, Address, Age);

            if (m_people.Count > 0)
            {
                MessageBox.Show("people count: " + m_people.Count);
                for (int i = 0; i < m_people.Count; i++)
                {
                   if(pSearch.getName().getFirstName() == m_people[i].getName().getFirstName() &&
                       pSearch.getName().getLastName() == m_people[i].getName().getLastName() &&
                       pSearch.getAge() == m_people[i].getAge() &&
                       pSearch.getAddress() == m_people[i].getAddress() &&
                       pSearch.getState() == m_people[i].getState() &&
                       pSearch.getZipCode() == m_people[i].getZipCode() &&
                       pSearch.getPhoneNumber() == m_people[i].getPhoneNumber())
                   {
                       Person p = m_people[i];
                       String person = "";
                       person += "First Name: " + p.getName().getFirstName() + "\n"
                                + "Last Name: " + p.getName().getLastName() + "\n"
                                + "Age: " + p.getAge() + "\n"
                                + "Address: " + p.getAddress() + "\n"
                                + "State: " + p.getState() + "\n"
                                + "Phone Number: " + p.getPhoneNumber() + "\n"
                                + "Zip Code: " + p.getZipCode() + "\n";
                       MessageBox.Show(person, "Found", MessageBoxButtons.OK);
                   }
                   else
                   {
                       MessageBox.Show("Could not find entry");
                   }
                }
                TBFirstNameSearch.Text = "";
                TBLastNameSearch.Text = "";
                TBAgeSearch.Text = "";
                TBZipCodeSearch.Text = "";
                TBAddressSearch.Text = "";
                TBStateSearch.Text = "";
                TBPhoneNumberSearch.Text = "";
            }
            else
            {
                MessageBox.Show("There's nothing in the list. Size: " + m_people.Count);
            }

This is inside of button click event. Basically, after I add a new entry to the array, I search the list for that same item but it isn't found. The item is added, as I said. One quick print of the list/array. Getting the count of the list/array shows a number of 1 meaning the entry has been added. If you do a for-loop of the list / array, it'll print all entries. Searching is......not working. Please let me know how to fix this. This is from a simple address book GUI I decided to make since it's never a bad thing to know more than one language.

EDIT: Sorry, went on a rant there....

Advertisement

Your code is very verbose.

Any reason why you are not using properties? http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx

You can also override Equals() or the == operator to make testing for equality much simpler http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

As for your problem: Are you sure the order of the arguments on this line is correct?


Person pSearch = new Person(firstName, lastName, State, ZipCode, PhoneNumber, Address, Age);

EDIT:

more stuff to read:

http://msdn.microsoft.com/en-us/library/bb384054.aspx

Also remember to override GetHashCode if you override Equals!

Well, after some trial and error and taking the advice of Madhed into consideration, I've managed to fix it. turns out? I forget to set a value in the "addToList" method of the main form. Yup, it's always the simplest stuff that gets ya.

This topic is closed to new replies.

Advertisement