- Home /
What is setter and getters??,What is setter and getters
hi, i wanna ask about setter and getters what are they used for and an example of setter and getter
and thanks,,hi, can u give me information about setter and getters like in C# 1_ why we use thim 2_ example of this method and explan
and thanks,
Answer by ptdnet · Nov 27, 2011 at 05:19 PM
They are public functions that can be called to GET or SET the value of a private variable.
public myclass {
private int _score;
public int MyScore {
get { return _score; }
set { _score = value; }
}
}
So in the above example you can access MyScore instead of _score. Totally useless and a complete waste of time and speed in my opinion, but object oriented purists love them.
On the contrary, the concept of properties (which include setter and getter) is extremely useful - not in your example, of course, but when you need to do additional things when setting a variable value. The "variables" transform, eulerAngles, rotation, rigidbody etc. are actually properties, and have getters and setters way more complex than simple assignments - take a look at my answer.
I actually dislike them in this syntactical form, even when they do useful things. To me it just obfuscates things to have code that "magically" happens when by all appearances you just set a member variable, and I always prefer to have explicit GetVariable() and SetVariable(value) methods in any case where something other than simply getting/setting the value actually goes on under the hood, so people using the code later can instantly say "aha, I'm not just setting a variable, I'm calling a function which will execute with potential side-effects!"
is value = to the original value of the variable _score in set{_score = VALUE; }
value is the value being assigned to the property. Its a reserved word.
$$anonymous$$yScore = 5;
here value is 5. Its whatever is on the right hand of the assignment operator. The setter would translate into:
_score = 5;
Thank you pdnet, by this you saved me from C# rage quit.
Answer by Peter G · Nov 27, 2011 at 06:37 PM
I'm probably not going to say too much that hasn't already been said, but I'm going to add a few things. Properties are function calls for getting and setting a value. They are most useful for abstraction. Properties are an easy way of hiding implementation from other classes. @Aldonaletto gave a good example of when you would do this. Another good one is getting the length of a vector.
public float magnitude {
get {
return Mathf.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
}
This example provides 2 good reasons why to use properties:
Other classes can't see how we get the magnitude which is good b/c they don't need to know. They only need the final value. Abstraction.
It only has a getter. A getter means that other objects that access this can only read our value, they can't tell us to change anything. This is used in the lazy loading frequently. There are also properties with only setters.
If you use properties, its important to be consistent. Properties and variables are similar, but there not the same. Someone trying to figure out your code later will probably thank you for being consistent, especially if they are reflecting over it. A useful feature when the property would be a simple getter/setter:
public float Value {
get; set;
}
Another useful feature of properties is that you can put them in interfaces and abstract classes. This is an advantage they have to variables. My favorite example:
public abstract class Animal {
public abstract int Limbs { get; }
}
public class Dog : Animal {
public override int Limbs {
get {return 4; }
}
}
This way you can get the number of limbs on every animal and know that it is a safe operation.
Answer by aldonaletto · Nov 27, 2011 at 05:57 PM
One of the most interesting OOP ideas is the concept of property. A property looks like an ordinary variable, but the big difference is the use of getter and setter, routines that are internally called when you read or assign to a property. Most GameObject "variables" are actually properties, each one with its own getter and setter. In most cases, there exists an ordinary private variable that stores the property value, but the getter and setter do a lot of extra things when you read or assign to the property.
The parent variable, for instance, is a property. Probably the getter is a simple routine like in the @ptdnet example, that only returns the value of the private _parent (or whatever they named it) variable. The setter, on the other hand, is way more complicated: when you assign a transform to the parent variable, the setter must calculate and assign localPosition, localRotation and localScale, not to mention several other internal changes related to physics or to the game object structure. If it wasn't a property, you should do all this extra work by hand, instead of just assigning the value to parent (thanks God for properties!)
Most times, the getter is simpler than the setter, but not always: eulerAngles, for instance, is a completely virtual property; there's no such a thing like a private variable Vector3 _eulerAngles - the getter converts the rotation internal value to Euler angles, and returns a Vector3. The declaration of the property eulerAngles probably is something like this:
public Vector3 eulerAngles { get { return ConvertRotationToEuler(_rotation); } set { _rotation = ConvertEulerToRotation(value); } }Where the functions ConvertRotationToEuler and ConvertEulerToRotation are very complex routines that run behind the scenes.
If you just need to assign to a variable or read its contents, don't mind - like @ptdnet said, using setter/getter in this case is a complete waste of time. But if you need to do other things anytime this variable is modified or read, then a property is the answer.
I don't understand. Can't you return the result of a function call inside a regular function also?