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 Inheritance in Code

C# Inheritance: console coming back with difference to tutorial

Hello,

I've ran my code and can't see where what I've written deviates from the video so I need a second pair of eyes to assist.

Game.cs(13,17): warning CS0219: The variable `p' is assigned but its value is never used
Compilation succeeded - 1 warning(s)
3
False
True
False

As the console shows two 'false's and one 'true' when the top 'false' should be a true.

my code is as follows:

(Game.cs)

using System;

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

          Point x = new Point (4, 2);  

          Point p = x;

          map.OnMap (new MapLocation(0,0));

         Console.WriteLine(x.DistanceTo (5, 5));

          Console.WriteLine(x is MapLocation);
          Console.WriteLine(x is Point);

          Point point = new Point (0,0);
          Console.WriteLine(point is MapLocation);
        }
    }
}

(MapLocation.cs)

namespace TreehouseDefence
{
    class MapLocation : Point
    {

        public MapLocation (int x, int y) : base(x, y)
      {

      }

    }

}

1 Answer

andren
andren
28,558 Points

The issue is this line:

Point x = new Point (4, 2);  

The first test produces false because x is not a MapLocation, it is a Point since that is what you assigned to it.

The line should be:

Point x = new MapLocation (4, 2);  

The above line might look wrong since you define x as holding a Point object, not a MapLocation object. But it is valid code because MapLocation is a type of Point, due to how it's inheritance is setup. Which means that you can in fact treat it as a Point object.