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# C# Objects Inheritance Throwing Exceptions

Error: Point.cs(33,37): error CS0117: `TreehouseDefense.Point' does not contain a definition for `x'

This is my Point.cs file. I don't understand why I am getting an error that says it does not contain a definition for 'x'.

using System; using static System.Math;

namespace TreehouseDefense { class Point { public readonly int X; public readonly int Y;

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public int DistanceTo(int x, int y)
    {
        int xDiff = X - x;
        int yDiff = Y - y;

        int xDiffSquared = xDiff * xDiff;
        int yDiffSquared = yDiff * yDiff;

        return (int)Math.Sqrt(xDiffSquared + yDiffSquared);

        //This is the same code refactored
  //      return (int)Math.Sqrt(Math.Pow(X-x, 2) + Math.Pow(Y-y, 2);
    }  

    public int DistanceTo(Point point)
    {
        return DistanceTo(Point.x, Point.y);
    }
}

}

1 Answer

andren
andren
28,558 Points

I'm guessing it comes from this method:

public int DistanceTo(Point point)
{
    return DistanceTo(Point.x, Point.y);
}

Here you reference Point.x and Point.y which is incorrect on two counts. Firstly you reference the class Point when you are meant to reference the parameter that is passed in point. Secondly you reference x and y when the Point properties are called X and Y capitalized.

If you fix those issues like this:

public int DistanceTo(Point point)
{
    return DistanceTo(point.X, point.Y); // Changed Point to point and x/y to X/Y
}

Then I think your current error will go away.

Thank you! That did the trick.