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 trialAaron Selonke
10,323 PointsConstructor Suddenly looks like a Function
I Guess Constructors are methods, ok What are the fields here?
1) Why doesn't the Map parameter in MapLocation() have a field? I guess I am used to seeing a few fields declared, and then a constructor...
2) and then, there is 'THIS'
He mentions that 'this'
- "refers to the current object"
- "used to get the object that the method was called in -"(in a constructor) 'this' is the object that was being constructed
I thought this sounded like a riddle, than when I went to the MSDN documentation on 'this', It might as well have been in Japanese.
The OnMap method takes a Point as a parameter, is 'this' just the x and y variables from MapLocation parameter?
3)Why are we Throwing Exceptions, I thought the logic would be revealed to me if I just kept chugging a long, but no, Aren't exceptions things that we are trying to avoid? Why are we throwing them?
Sorry for the link of tenuously related questions, I think there were just a few too many unfamiliar concepts that came up suddenly
class MapLocation : Point
{
public MapLocation(int x, int y, Map map) : base(x, y)
{
if (!map.OnMap(this))
{
throw new System.Exception(x + "," + y + "is outside the boundaries of the map");
}
}
}
5 Answers
Steven Parker
231,198 PointsThe example of passing a value to a constructor that is not stored may be unusual, but it is perfectly legitimate. In this case, it is only used to perform the test to make sure the other values are good, so it doesn't need to be retained.
A constructor method will always have the same name as it's class. Also, it will never return anything (other methods might not return anything either, but they will be declared "void").
Loading fields is certainly the most common purpose of a constructor, but it's not required. In this case the x and y fields do get loaded, by calling the base (which is Point).
The catch would not be in the class that throws the exception, but you would use try and catch in the code that uses that class. Sometimes the purpose is to show messages, and sometimes the calling code will handle the exception some way that doesn't involve a message. One possibility is that when an exception is thrown, it would choose different values and try again.
Steven Parker
231,198 PointsYes, constructors are special kinds of methods. And methods are functions.
MapLocation doesn't have any fields because it inherits from Point. Think of it as a special kind of point. What makes it special is a behavior (method) that a regular point doesn't have, and a special check it makes when you construct it.
Inside MapLocation, this refers to a specific instance of MapLocation. Inside the constructor, it's the instance that is being constructed. So you're right, when it is passed to OnMap, it is so the x and y can be checked.
The code that uses MapLocation will certainly want to avoid the exception. But one is thrown inside the constructor just in case the code using it accidentally gave it point values (x and y) that are not good for the map. Think of it like a circuit breaker. You don't want to blow them, but when you build a house, you put them in for safety.
Did that make things clearer?
Jennifer Nordell
Treehouse TeacherJust a note about methods vs functions. All methods are functions but not all functions are methods. Method is just a fancy way of saying a function that is specifically tied to an object.
Aaron Selonke
10,323 PointsSo far I've been using the terms interchangeably based on how they sound aesthetically in the sentence=) (I thought that a function tied to an object - was the difference between Static and Instance methods)
Jennifer Nordell
Treehouse TeacherAaron Selonke hehehe not technically correct. Unless you're maybe coding in Java where everything is an object. If you thought about it... it was probably an object
Aaron Selonke
10,323 Points1) Seems illogical to a newbie like me that there are no fields to handle for the Map variable in the Maplocation class (or Point class) It seems like to have the Map in the constructor is inconsistent with the class rules that I knew up until now. Moreover, Even-though the MapLocation method has the same name as the class, it acts more like a normal function and not a constructor method ( which I thought was to cookie cut objects from the class fields which were explicitly listed) Ill just move on, and eventually will see this as normal
2) ok
3) So when we code to throw an exception - there should always be a corresponding 'catch' so we can write a user friendly message to the user? Is that what they are mostly for?
Thanks for the quick response
Thank you
Aaron Selonke
10,323 PointsRight On. Thanks