- Home /
Are there Indexed Properties in Unity Javascript?
Hi, I see from an answer here that Unity Javascript supports C# properties. Can it also support C# Indexed Properties (as described here)? That is can I somehow write a Unity Javascript class like the C# class below:
class Employee {
public double this[int year] {
get { // return employee's salary for a given year. }
}
}
I tried guessing a few syntax combinations, but none would compile. Is this possible?
Answer by Bunny83 · May 02, 2012 at 11:16 AM
No, UnityScript ia a proprietary language which is build on top of Mono, but they didn't support all features. You can declare "normal" properties, but only when you have an explicit class construct around it and only simple, non-indexed.
I've tried different ways to declare an Indexer like you want and had no luck yet ;)
This is a rarely used feature of C# so i don't think they implement it in UnityScript. You have to use methods instead.
btw. your Employee example is not very convenient ;)
A good use of an indexer is the Vector3 struct:
public float this[int index]
{
get
{
switch (index)
{
case 0: { return this.x; }
case 1: { return this.y; }
case 2: { return this.z; }
default:{ throw new IndexOutOfRangeException("Invalid Vector3 index!");}
}
}
set
{
// [...]
}
}
Your answer
Follow this Question
Related Questions
Why not Boo? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Can I declare properties in JS? 1 Answer
Accessing properties from variable 1 Answer
Is something wrong with Boo? 4 Answers