- Home /
How can I declare class properties of an object in a single line?
Instead of populating a new class variable by declaring its properties one at a time, is it possible to do it in a single line?
I.e., Rather than…
var myClassInstance : MyClass;
myClassInstance.name = "a name";
myClassInstance.place = "a place";
myClassInstance.number = 32;
…could I do something like the following instead?
var myClassInstance = new MyClass( "a name", "a place", 32 );
Answer by poncho · Jan 16, 2014 at 02:27 PM
in c# it is
MyClass myClassInstance = new MyClass(){name = "a name", place = "a place", number = 32};
not sure about JS but you could try
Answer by Jessy · Jan 16, 2014 at 02:14 PM
I use object initializers for Unity objects and structs, because we can't make more constructors for them. I rarely use it for my own classes/structs.
http://msdn.microsoft.com/en-us/library/bb384062.aspx
Please tell us if this works in UnityScript. I can't recommend using UnityScript for any reason, but I'm interested.
Edit: I didn't realize you didn't know about constructors. Learn those first.
http://wiki.unity3d.com/index.php/UnityScript_versus_JavaScript#JavaScript_is_class-free
didnt check'd the links, but this answers pretty much covers the question, upvoted and please select it as the answer
Thanks for the links.
So I'm looking into constructors (which you correctly recognized I didn't know about) and they look a useful tool that I'm going to keep learning about. But just so I understand your answer, are you saying that what I want — basically poncho's answer — is not possible to do in UnityScript without using a constructor? Or are you saying that you don't know if it's possible and you're suggesting I should learn how to do it via a constructor?
the code I posted is the same as typing
$$anonymous$$yClass myClassInstance = new $$anonymous$$yClass(); myClassInstance.name = "a name"; myClassInstance.place = "a place"; myClassInstance.number = 32;, having the properties or public variables inside the {} its a shortcut for myClassInstance.{propname}, a constructor "myClass()" its the basic class constructor, an overloaded constructor "myClass("a name", "a place", 32)" would need you create a constructor, in the myClass class, a public myClass(string mynamevalue, string myplacevalue, int mynumbervalue) where you only assing the parameter to the value in the object
But like I said, there's really no reason to do this with your own classes. If constructor overloading isn't enough, because you have too many parameters of the same type, then you can use default parameters, and na$$anonymous$$g. var myClassInstance = new $$anonymous$$yClass(name: "a name", place: "a place", number: 32);
@Essential you should learn how to write constructors, and then, if you still want to use UnityScript, find out if you can use object initializers in UnityScript. They can yield very concise, yet descriptive, code.
Your answer
Follow this Question
Related Questions
UnityScript - Class reference 1 Answer
Are Unity's JS OOP functions customized? 2 Answers
How to correctly convert Object to GameObject 1 Answer
Please help with this code!! 1 Answer