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 Properties

douglas gray
douglas gray
888 Points

many steps to make a short method. completely lost.

Hey all! I can't seem to explain this to myself at all. I see that Jeremy creates a private variable called _location. I can't seem to remember why we would do that at all if the public method below calls for us to change the values of that variable. I guess i don't really understand what it means to "overwrite a field".

Next, I see that he creates a GetLocation method in which he "gets" the return value of _location (or is "getting" the value simply returning the value to the computer ?) and "sets" the _location variable equal to some value. Why is he doing all of this? why are there so many steps and what is the specific purpose of each one? if anyone could rephrase some of what he says/flesh it out a bit i would greatly appreciate it. Thank you so much for your time.

Matthew McGuff
Matthew McGuff
4,355 Points

Remember Jermey discussed in the video the design principle of encapsulation. This overly idea behind this design principle is that the user of an object, method or class doesn't have to know exactly how it works in order to use it. A driver of a car isn't given access to the exact amount of fuel is put into the engine to make it go forward. We have a gas pedal.

We want to use private variable and use the getter and setter for the same reason the car manufacture does want to require driver to define the exact measurement of fuel to make the car go forward. We are trying to keep it simple for the user. By keeping this variable private we prevent it from being altered from another place in code other than where its intended to be accessed. We keep everything under the hood as it were for others writing code.

This principle of encapsulation doesn't make a ton of sense when consider writing code for just yourself. But when you consider that others may use your code and want to make it do other things then protecting how it function with getters and setters make a ton of sense. Hope that helps.

3 Answers

Steven Parker
Steven Parker
229,732 Points

:point_right: The methods may appear redundant because they are.

The video shows different ways to accomplish limiting access to an internal variable. You'll notice that the access level of the variable is private, so direct access is not allowed outside the class.

The GetLocation method does the same thing as the Get method in Location property. In actual practice, you would not do both in the same class, but they are shown here as examples.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Douglas,

It sounds like you are talking about "Getters" and "Setters," which a pretty important in compiled languages. the short answer is you may not want people changing a variable directly, but may need access to 'get' it or modify it for another variable. All of which are very complex explanations and probably beyond the scope of the Community forums for a detailed answer.

I don't do much with C#, but getters and setters are very common in Java. Here is a link for the C# Docs, but also have a read from some of the Stack Overflow threads. They aren't necessarily for C# so the syntax may be different, but the general reason as to why are the same. Why use Getters and Setters or How do Getters and Setters ...

I hope those will help some in clarifying why we use them. :)

:dizzy:

Hello i'm having the same problem i found a YT video that explains this more in depth https://www.youtube.com/watch?v=gvQziNULkdY