Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Intermediate C# System.Object Object.Equals

drstrangequark
drstrangequark
8,273 Points

What am I doing wrong here?

I am being asked to override the Equals method. This is based on the C# video called Object.Equals. I followed the override format that was in the video almost exactly, just changing where the video said Point to VocabularyWord in this exercise. However it keeps telling me that it still returns false when the two words are the same. What am I missing here?

VocabularyWord.cs
using System;
namespace Treehouse.CodeChallenges
{
    public class VocabularyWord
    {
        public string Word { get; private set; }

        public VocabularyWord(string word)
        {
            Word = word;
        }

        public override bool Equals(object obj)
        {
            if(!(obj is VocabularyWord))
            {
                return false;
            }

            VocabularyWord that = obj as VocabularyWord;

            return this == that;
        }

        public override string ToString()
        {
            return Word;
        }
    }
}

1 Answer

Steven Parker
Steven Parker
229,582 Points

You're really close, but in your code both "this" and "that" represent VocabularyWord objects. The instructions ask that you compare "the words of two VocabularyWord objects", but the code here is directly comparing the objects themselves.

drstrangequark
drstrangequark
8,273 Points

Thanks! I changed that one line to return this.Word == that.Word and it worked!