Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Methods Static vs Instance Methods

Azim Sodikov
Azim Sodikov
11,432 Points

DistanceTo method is giving error!

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

I build my local environment to build this, it was working just fine. However, this code is giving a error! Can anybody help me out?

Point.cs(14,20): error CS0161: 'Point.DistanceTo(int, int)': not all code paths return
e [/Users/azimjonsodikov/workspace/BackEnd/Csharp/exercises/DefenseGame/DefenseGame.cs

1 Answer

Seth Kroger
Seth Kroger
56,403 Points

You're getting this error because you're saying DistanceTo() returns an int, but you aren't returning any number. It looks like you forgot the final lines to calculate the distance from the squares and return the result.