Be a Supporter!

Accessing a class like array

  • 141 Views
  • 4 Replies
New Topic Respond to this Topic
coln
coln
  • Member since: Aug. 1, 2011
  • Offline.
Forum Stats
Member
Level 01
Musician
Accessing a class like array 2013-12-06 12:34:21 Reply

So I was just messing around in flash and was wondering if there was a way to access any class properties with array notation.

Say I have class Test:

public class Test {
   public static var one:int = 1;
   public static var two:int = 2;
   public static var three:int = 3;
   
   public function Test() {
   }
}

I could access those values like Test.one, however I was wondering if there was a way to access them like Test[0], where Test[0] would output "1."


BBS Signature
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to Accessing a class like array 2013-12-06 12:40:07 Reply

Why don't you push the variables into an array inside of your Test class and access them that way?

coln
coln
  • Member since: Aug. 1, 2011
  • Offline.
Forum Stats
Member
Level 01
Musician
Response to Accessing a class like array 2013-12-06 12:44:22 Reply

At 12/6/13 12:40 PM, Sam wrote: Why don't you push the variables into an array inside of your Test class and access them that way?

That is an option, definitely, but I was just curious if my above question was possible. I don't think I would ever need to use it, haha.


BBS Signature
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to Accessing a class like array 2013-12-06 13:07:09 Reply

At 12/6/13 12:44 PM, coln wrote:
At 12/6/13 12:40 PM, Sam wrote: Why don't you push the variables into an array inside of your Test class and access them that way?
That is an option, definitely, but I was just curious if my above question was possible. I don't think I would ever need to use it, haha.

I've just given it a go by creating a class that extends Array. Of course you get all the methods such as push and pop, but you can't access values via square brackets as easily, as far as I can tell. Maybe someone with a bit more knowledge can give you a better answer.

Some reading that you might be interested in: Extending the Array Class and Data Access with array and proxy extending.

coln
coln
  • Member since: Aug. 1, 2011
  • Offline.
Forum Stats
Member
Level 01
Musician
Response to Accessing a class like array 2013-12-06 14:11:29 Reply

At 12/6/13 01:07 PM, Sam wrote: Some reading that you might be interested in: Extending the Array Class and Data Access with array and proxy extending.

Very interesting indeed. Thanks for the reply!


BBS Signature