- Home /
boolean questions get, set, in java.
so I was watching a video one the made a Boolean in c# that looked like-
public bool name { get; set; }
how is that different then a true false, and how do I write that in JavaScript?
Answer by maccabbe · Mar 16, 2015 at 11:36 PM
There is no difference to a beginner programmer between
public bool name{get; set;}
and
public bool;
The difference between these two lies in one being a field while the other is a property. Properties are used to hide fields while allowing outside classes to edit them like fields and to customize getting and setting fields. For instance, you can write methods that are run when doing so and you can even make one private and the other public. Also fields are treated differently than properties under certain conditions. The most notable is that you can't pass properties as ref or out parameters (which would defeat the purpose of hiding the field).
private bool _name { get; set; }
public bool name {
get{return _name;}
set{_name=value;}
}
In javascript I believe you have to use methods instead of accessors (get/set) to get the same effect, i.e. something like this
private bool name;
public GetName(){
return name;
}
public SetName(bool value){
name=value;
}
Now it is easier to edit a property than change a field to a property. If a field is changed to a property then any code that accesses that field from the outside must be recompiled. However if a property is edited then that code might not need to be recompiled. Also since fields are treated differently than properties code might not even work if you change a field to a property.
thanks for the explanations, but in java it would look like this.
function Getname () { return name; }
function Setname (value : int) { name = value;}
In uJS they are declared in separate functions (in which you should include the keyword 'function') but they are accessed in the same way as C#.
#pragma strict
public class Player
{
//$$anonymous$$ember variables can be referred to as
//fields.
var experience : int;
//Experience is a basic property
public function get Experience() : int
{
return experience;
}
public function set Experience(value : int)
{
experience = value;
}
}
#pragma strict
function Start ()
{
var myPlayer = new Player();
//Properties can be used just like variables
myPlayer.Experience = 5;
var x : int = myPlayer.Experience;
print(x);
}
http://unity3d.com/learn/tutorials/modules/intermediate/scripting/properties
Your answer
![](https://koobas.hobune.stream/wayback/20220613190342im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
PlayerPref/PlayerPrefsX why not able to store boolean values? 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Else and If 2 Answers
c# setter/getters 1 Answer
Public variable hidden in the inspector 2 Answers