C# C# Objects Inheritance Throwing Exceptions

Sheryl Darroch
Sheryl Darroch
Treehouse Project Reviewer

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
26,954 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.