Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Xavier78 · Mar 16, 2015 at 10:08 PM · javascriptbooleangetset

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xavier78 · Mar 17, 2015 at 01:06 AM 0
Share

thanks for the explanations, but in java it would look like this.

 function Getname () { return name; }
  
 function Setname (value : int) { name = value;}
avatar image meat5000 ♦ · Mar 17, 2015 at 01:28 AM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges