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

Derian McCrea
Derian McCrea
1,370 Points

Simple question (I hope) regarding creating public vars and when to use Caps or lower case in methods.

I know this will eventually bite me in, but I can't relate a "hard rule" to remember when to use a Caps eg Height and a lowercase height. look for "//" comments

for example:

namespace TreehouseDefense {

class Map
{
  // int Width and int Height are defined public vars, I understand to declare a new var 
  // they should have Caps at the start.
  public readonly int Width;
  public readonly int Height;

  public Map(int width,int height)
  {

        // now I think they are defining them here aka instantiating the variable to be used 
        // for the Map Constructor...? yes no?
        Width = width;
        Height = height;
  }

  public bool OnMap(Point point)
  {

       // But here why would Width and Height in this operation be Caps? Is it because the        
       // of the scope of the lower case width and height remain in the Map constructor?
       // So to use these public vars in other equations have to be called on by using Caps?
      return point.X >= 0 && point.X < Width && 
             point.Y >=0 && point.Y < Height;

  }


  }

}

2 Answers

Yes, you are right in all points and comments:

  • the scope of the parameter is limited to through the brackets of the constructer;
  • to use the Width and Height outside of the constructer you have to capture them by creating copies;
  • to use a variable outside of a class or method you have to define public fields (or properties);
  • your public vars are called pubic fields;
  • public properties begin with a capital letter (convention);
Derian McCrea
Derian McCrea
1,370 Points

Thanks for taking the time to figure out my notations and clarifying the process!

Matti Mänty
Matti Mänty
5,885 Points

The reason why the Width and Height start with a capital letter in OnMap(Point point) method is because the variables names are case sensitive. The variables defined for the class are Height and Width, so whenever those are referenced within the script, you must use the same formatting.

In public Map(int width, int height) however, int width and int height are parameter values when creating a Map object. They could be written with the same casing, but it's easier to distinguish (and less chance of making an error with) the object's/class' variables from the parameters if they are not identical.

So, the Width and Height are used in the class/object script's methods. Lowercase width and height are only used when a Map object is being created from another script, like:

int width = 5;
int height = 10;
public Map mapObject = new Map(width, height);
Derian McCrea
Derian McCrea
1,370 Points

Thanks for taking the time to clarify!