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. Using Static Methods. DistanceTo() method returning strange value. -2147483648

I'm not sure where my code differs from the instructor.

// Point.cs

using System;

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);

    }
}

}

// Game.cs

using System;

namespace TreehouseDefense { class Game { public static void Main() { Map map = new Map(8, 5);

        Point point = new Point(4, 2);

        Console.WriteLine(point.DistanceTo(5, 5));
    }
}

}

2 Answers

Steven Parker
Steven Parker
229,608 Points

Adding a value to itself doubles the value, it does not square it. Did you perhaps intend to use the * symbol instead of +?

And for future postings, always include a link to the course page you are working with (or use the "get help" button which adds one for you).

Also, use Markdown formatting to preserve posted code's appearance and retain special symbols. An even better way to share code and make your issue easy to replicate is to make a snapshot of your workspace and post the link to it here.

Markdown. Got it. Thanks. Thanks a lot.