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];
DaveA
2
You could use a HashTable, like an array where the index can be a string. Or look into ‘eval’ on the MSDN
jahroy
3
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?
system
4
You can create custom indexers in C#. Details Here