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

Tyler Marquer
Tyler Marquer
12,659 Points

i don't understand whats wrong with my code

says it needs a constant value and a few other errors and warnings that i dont know how to fix

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(word is word)
            {
                return true;
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
217,443 Points

There are a few issues here:

  • the function still needs to return a bool value when the condition is not met
  • the internal attribute is "Word" (with capital "W") instead of "word"
  • the internal "Word" needs to be compared with the one in the obj
  • the object will need to be cast as a VocabularyWord to access its attribute
  • you'll want to use a separate test or the null coalescing membership operator to avoid errors with null objects
  • the comparison should be for equality ("==") instead of identity ("is")