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 Loops and Final Touches Magic Numbers and Constants

Sean Flanagan
Sean Flanagan
33,235 Points

Error in Invader.cs

Hi. I got to the end of this video, but when I tried to compile, there was an error in my Invader.cs file.

Invader.cs(12,58): error CS1061: Type `TreehouseDefence.Path' does not contain a definition for `Length' and no
 extension method `Length' of type `TreehouseDefence.Path' could be found. Are you missing an assembly referenc
e?                                                                                                             
Path.cs(3,9): (Location of the symbol related to previous error)

Here's a snapshot of my workspace:

https://w.trhou.se/nxdvriuafs

I would appreciate any help.

Sean

10 Answers

Hi Sean Flanagan Here are some extra errors I found, noting crazy, but after this it should work.

  1. In Path.cs like in the comment you need to add the public int into your class, like in the comment provided.

  2. In Invader.cs you had the line:

    public bool HasScored {get {return _pathStep = _path.Length;}}

this needs to be changed to

    public bool HasScored {get {return _pathStep >= _path.Length;}}

add the greater than equals operator >=. after those changes it should work.

Hi Sean, your code looks really good! only one thing that was triggering the error.

In your Invader.cs file:

you wrote:

public void Move() => _pathStep += 1;

you need to change that to

 public void Move() => _pathStep++;

The error was caused by the increment. If you want to lean more I suggest you read this post

What is the difference between “++” and “+= 1 ” operators?

If you run into any problems remember you can always ask here. Happy Coding!

Note: The bug should be located at line 24 in your Invader.cs file.

Liam Andersson
Liam Andersson
1,396 Points

Seems like you missed to declare "Length" to something. I guess that is the error, I would suggest you go through the code and look if you defined Length.

Sean Flanagan
Sean Flanagan
33,235 Points

I've looked through my code and I can't find anything out of place.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Jacob. Thanks for the reply. I've given you an up vote.

I've changed line 23 of Invader.cs to:

public void Move() => _pathStep++;

Is this right? It's just that I'm still getting the same error message.

On going over the code again, I also found soem other things you were missing. I will follow up with a revised answer shortly. one this is you do not have

namespace TreehouseDefense
{
    class Path
    {
        private readonly MapLocation[] _path;

        public int Length => _path.Length; // this was not in your workspaces. 

        public Path(MapLocation[] path)
        {
            _path = path;
        }

        public MapLocation GetLocationAt(int pathStep)
        {
            return (pathStep < _path.Length) ? _path[pathStep] : null;
        }
    }
}
Sean Flanagan
Sean Flanagan
33,235 Points

I made both adjustment. However, when I compiled, I didn't get any message in the console. Is that normal?

No you should of gotten a complied complete message. can you please upload your recent workspaces.

This workspace is not updated with the answers provided. Please update the workspace and try again. If you still have an issue with compiling, please add the updated workspace and please let me know what changes you made along with any errors you might have.

Sean Flanagan
Sean Flanagan
33,235 Points

I'm not sure how to do this. I've refreshed the workspace and compiled again but same result. No error messages.

Path.cs:

namespace TreehouseDefence
{
  class Path
  {
    private readonly MapLocation[] _path;

    public int Length => _path.Length; // added as you suggested

    public Path(MapLocation[] path)
    {
      _path = path;
    }

    public MapLocation GetLocationAt(int pathStep)
    {
      return (pathStep < _path.Length) ? _path[pathStep] : null;
    }
  }
}

Invader.cs:

namespace TreehouseDefence
{
  class Invader
  { 
    private readonly Path _path;
    private int _pathStep = 0;

    public MapLocation Location => _path.GetLocationAt(_pathStep);

    public int Health {get; private set; } = 2;

    public bool HasScored {get {return _pathStep >= _path.Length;}}

    public bool IsNeutralised => Health <= 0;

    public bool IsActive => !(IsNeutralised || HasScored);

    public Invader(Path path)
    {
      _path = path;
    }

    public void Move() => _pathStep++; // changed to ++ as suggested

    public void DecreaseHealth(int factor)
    {
      Health -= factor;
    }
  }
}

take a look at the edited snapshot. snapshot This will compile your code with a warring, that is also found in the instructors version as well.

The snapshot provided is working on my end. If you are still having issues, what command are you using in the console to compile?

Sean Flanagan
Sean Flanagan
33,235 Points

mcs -out:TreehouseDefence.exe *.cs

with the code provided are you still getting not receiving an compiled response in the console?

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Jacob. I started combing through my code, all the files and comparing it with yours. I went to Game.cs and found two lines that shouldn't have been there, lines 27 and 28:

  MapLocation location = path.GetLocationAt(8);
  Console.WriteLine(location.X + "," + location.Y);

I deleted these lines and compiled my code again and got this message:

Game.cs(13,18): warning CS0219: The variable `path' is assigned but its value is never used                    
Compilation succeeded - 1 warning(s) 

It's working now. Thank you for your help.

Any time! Glad you got it working.