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 Encapsulation with Properties Property Practice

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Compilation Fail in DecreaseHealth Method.

In the video there is no return keyword in the DecreaseHealth method, but when I complie I got this messages. I solved by typed return inside DecreaseHealth method, and also how to get rid of this warning. Much Appreciate!

Game.cs(17,10): warning CS0219: The variable `path' is assigned but its value is never used

Invader.cs(65,14): error CS0161: `TreehouseDefense.Invader.DecreaseHealth(int)': not all code paths return a value

Compilation failed: 1 error(s), 1 warnings

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! First, this would be much easier to help you with if we could see your code. You could either post it here using the instructions found in the Markdown Cheatsheet link at the bottom of the "Add an Answer" section or you could link us a snapshot to your workspace. You can create a snapshot by clicking the camera icon on the upper right hand side of your workspace.

My best guess as to why you've received the error and fixed it with a return statement is because the DecreaseHealth function was not assigned a return type of void. If the return type of the method is not void the compiler will expect the return of a value on all possible logical paths.

As for the warning, I'm actually going to need to see your code. But somewhere you've declared a variable named path, but then never used it.

Hope this explains the return error, but I look forward to seeing your code so that I can help with the warning! :sparkles:

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Thank You very much!

Here my code. On Game class

using System;

namespace TreehouseDefense
{
    class Game
    {
        //static because this can call without create object
        private static void Main()
        {
            //Declare variable map with type Map and assign with newMap object.
            //this new object need values for it to be created
            Map map = new Map(8, 5);

            try
            {
                //Pass MapLocation array into path constructor
                Path path = new Path(
                    new [] {
                        new MapLocation(0, 2, map),
                        new MapLocation(1, 2, map),
                        new MapLocation(2, 2, map),
                        new MapLocation(3, 2, map),
                        new MapLocation(4, 2, map),
                        new MapLocation(5, 2, map),
                        new MapLocation(6, 2, map),
                        new MapLocation(7, 2, map)
                    }
                );



            }
            //Start with most specific
            catch(OutOfBoundsException ex)//ex is an object of OutOfBoundsException, by declare it
            {
              //access message inside ex
                Console.WriteLine(ex.Message);
            }
            catch(TreehouseDefenseException)
            {
                Console.WriteLine("Unhandled TreehouseDefenseException");
            }
            //To the most general
            catch(Exception ex)//This exception can handle all the exception that occured, because it main
            {
                //There are infomation inside ex variable so we can use them
                Console.WriteLine("Unhandled Exception: " + ex);
            }
        }
    }
}

On Invader class

namespace TreehouseDefense
{
    class Invader
    {
        //keep track, where the invader is
        private int _pathStep = 0;
        //Use to store path instance
        private readonly Path _path;


        //Public property is better, bacause we shold make field private
        public int Health { get; private set; } = 2;

        //Auto-Property, no backup field but still can use get and set
        public MapLocation Location
        {
            get
            {
                return _path.GetLocationAt(_pathStep);
            }
        }

        //Tell whether the invader is reached the end of the path or not
        public bool HasScored
        {
            get
            {
                //Check with Path.Length Property
                return _pathStep >= _path.Length;
            }
        }

        //Use for Check if invader is death or not for other class to know
        public bool IsDeath
        {
            get
            {
                //Check with Path.Length Property
                return Health == 0;
            }
        }


        public bool IsActive
        {
            get
            {
                //If return false means Death, or Reached the end. True means isn't Death, or isn't reach the end. 
                return !(IsDeath || HasScored);
            }
        }

        //Constructor for initialize the LocationProperty
        public Invader(Path path)
        {
            _path = path;
        }

        //Use this method to move the invader
        public void Move()
        {
            _pathStep += 1;
        }

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

    }
}

Much Appreciate

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

You right! I should type void like the video but I typed int instead. Thank you!!!