x


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?

more ▼

asked May 02 '12 at 10:27 AM

cordinc gravatar image

cordinc
169 2 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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
    {
        // [...]
    }
}
more ▼

answered May 02 '12 at 11:16 AM

Bunny83 gravatar image

Bunny83
45k 11 48 206

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3442
x75
x21

asked: May 02 '12 at 10:27 AM

Seen: 460 times

Last Updated: May 02 '12 at 11:16 AM