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 trialdrstrangequark
8,273 PointsWhat 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?
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
231,275 PointsYou'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
8,273 Pointsdrstrangequark
8,273 PointsThanks! I changed that one line to return this.Word == that.Word and it worked!