x


Dynamically Reference a Variable in Class?

How can I reference a public variable in a class NOT using dot syntax, but some way where I can use a dynamic variable name?

For example, if I have an object like this:

class DataObject{ 
    public var speed:float;
    public function DataObject(){  
       speed=55;
    }
}  

I know I can reference speed like this:

var dataObject=new DataObject();
var curSpeed=dataObject.speed;

but I want to reference it like this:

var varName="speed";
var curSpeed=dataObject[varName];  
more ▼

asked Nov 09 '11 at 03:15 PM

davedev gravatar image

davedev
594 37 41 49

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

3 answers: sort voted first

You could use a HashTable, like an array where the index can be a string. Or look into 'eval' on the MSDN

more ▼

answered Nov 09 '11 at 10:00 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

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

You could easily do what you want using reflection, but I think it would introduce a performance hit and increase the size of your executable.

I believe it's possible to override the [] operator in C# (if you must use that exact syntax) but I'm not sure.

Just out of curiosity... Why do you ask?

Is it because you're trying to translate and/or work with php code?

more ▼

answered Nov 09 '11 at 10:17 PM

jahroy gravatar image

jahroy
3.2k 14 18 41

It's definitely possible to override the [] operator in C#, but yeah, the question still remains- why?

Nov 09 '11 at 10:28 PM syclamoth

@syclamoth: It's not really possible to overload the [] operator in C# but you can define an indexer which looks quite similar. That's what the Hashtable / Dictionary uses.

Nov 09 '11 at 10:51 PM Bunny83

I've been working in Flash so I'm used to throwing just about anything into arrays and being able to use one of set of functions to find things such as all the visible display objects, all data objects containing certain values, etc.

Nov 09 '11 at 11:03 PM davedev

I won't claim to be Mister Optimization, but I do know you can expect a performance hit if you're constantly using string values to reference variables.

I suspect that doing what you want to do might go against the whole point of using a game engine: code optimized for graphics.

I would think it might be pretty easy to implement some of these handy functions yourself (in a way that fits Unity).

I think php array/hashtables are awesome... But they're for php projects.

Nov 09 '11 at 11:20 PM jahroy
(comments are locked)
10|3000 characters needed characters left

You can create custom indexers in C#. Details Here

more ▼

answered Nov 10 '11 at 09:57 PM

gruhm gravatar image

gruhm
121 1 2

(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:

x337
x109

asked: Nov 09 '11 at 03:15 PM

Seen: 538 times

Last Updated: Nov 10 '11 at 09:57 PM