Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# Intermediate C# System.Object Object.Equals

Stuck trying to override the Equals(object obj) method. Here is my code:

I'd been trying for a while now and I am just spinning my wheels at this point, any help is greatly appreciated, here is the code:

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

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

        public override string ToString()
        {
            return Word;
        }

        // this is my code - the one that does not work :( 
        public override bool Equals(object obj)
        {
            if ( !(obj is Word) )
            {
                return false;
            }

            Word that = obj as Word;

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

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

        public override string ToString()
        {
            return Word;
        }

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

            Word that = obj as Word;

            return this == that;
        }
    }
}

1 Answer

andren
andren
28,521 Points

You seem to have slightly misunderstood the challenge.

The name of the class is VocabularyWord not Word. Word is a property that exists on the VocabularyWord class.

So the equal method is meant to check if the obj is a VocabularyWord instance and then cast it as such if that is the case. Then return true or false based on whether the Word properties of the two objects match up.

Since most of your code is fine besides the naming confusion all you need to do to fix your code is to change all references of Word to VocabularyWord, and then to add .Word to your comparison. Like this:

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

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

        public override string ToString()
        {
            return Word;
        }

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

            VocabularyWord that = obj as VocabularyWord;

            return this.Word == that.Word;
        }
    }
}

Awesome, thank you. I had it with the VocabularyWord class originally, but I changed it trying to fix the issue. All I was missing was the .Word property call on the return statement. Thank you for the quick response.