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#

Same code as video, different result?

I believe that I have typed everything exactly the same (except for variables) as in the C# video "Using Static Methods" under Methods in C# Objects course, but when I run my program, I get -2147483648?

Here is my code:

using System;

namespace TreehouseDefense

{

class Point


{
    public readonly int xLocation;
    public readonly int yLocation;

    public Point(int x, int y)
    {
        xLocation = x;
        yLocation = y;
    }

    public int DistanceTo(int x, int y)
    {
        int xDifference = xLocation - x;
        int yDifference = yLocation - y;

        int xDiffSquared = xDifference + xDifference;
        int yDiffSquared = yDifference + yDifference;

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

}

3 Answers

I don't think this is the formula to square the distance. Try an asterisk. int xDiffSquared = xDifference + xDifference; int yDiffSquared = yDifference + yDifference;

int xDiffSquared = xDifference * xDifference; int yDiffSquared = yDifference * yDifference;

Seth Kroger
Seth Kroger
56,413 Points

Squared means multiplied by itself, not added.

Yep that's it! Looked back at the video and found out that he multiplied the difference to get the squared, not add. My bad. Thank you for the help!