FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   how to dynamically acess an object variable?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: how to dynamically acess an object variable?  (Read 478 times)
lunetta

7200475272004752 WWW Email
how to dynamically acess an object variable?
« on: May 9th, 2004, 8:20pm »

Hello all
 
this topic has a complicated title for a simple question;
 
check this example:
 
class prop {
  int x,y,w,h;
  prop(int xx, int yy, int ww, int hh){
    x=xx;
    y=yy;
    w=ww;
    h=hh;
  }
}
 
void check(prop o, char m) {
println(o.m);
}
void setup() {}
void draw(){
prop test = new prop(10,15,20,30);
check(test, 'x');
}
 
 
It obviously doesn't work;
what I really need is to make a function access a certain variable of the object. Is there any way to it dynamically? Which datatype should I define on the function?
 
Thanks and sorry for so many questions!
 
 
PS: I was calling the object variable as a property, and this was a little confusing, so I edited...
« Last Edit: May 9th, 2004, 9:17pm by lunetta »  
mflux

fluxhavoc WWW
Re: how to dynamically acess a object property?
« Reply #1 on: May 9th, 2004, 8:46pm »

println(o.m);  
 
That line is trying to access a variable called "m" within the prop class structure, when there is no variable called m existing anywhere inside the structure.
 
lunetta

7200475272004752 WWW Email
Re: how to dynamically acess a object property?
« Reply #2 on: May 9th, 2004, 8:51pm »

I know it doesn't work;
What I really want is to access the x variable on that object;  
 
when I call the function check(test, 'x') my intention is that this function resolve 'x' as being a variable name; test.x.
In the original program I can't simply call test.x in the function statement because things are a little more complicated - I'm dealing with multiple object arrays; if I don't find a way to dynamically resolve a variable name in certain object, I'll have to create a different function to each variable...
 
sorry if this is confusing...  
 
thanks!
 
 
ps: I edited trying to be more clear...
« Last Edit: May 9th, 2004, 9:06pm by lunetta »  
kevinP

Email
Re: how to dynamically acess a object property?
« Reply #3 on: May 9th, 2004, 10:19pm »

on May 9th, 2004, 8:51pm, lunetta wrote:
In the original program I can't simply call test.x in the function statement because things are a little more complicated - I'm dealing with multiple object arrays; if I don't find a way to dynamically resolve a variable name in certain object, I'll have to create a different function to each variable...

 
I'm not sure (therefore interested to see answers), but I think that you will go down a dark and murky programming hole if you try to pass references to variable _names_...
 
My naive hunch would be to ask whether you can access these via array indexes (hmm... 'indices')
 
Code:

 // data is stored in myList[]
 // a method in class prop
 // that prints array value for given index
 void printListValuesFor(int x) {  
   println(myList[x]);  
 }  
 
 test.printListValuesFor(2);  

I think that this might work.
 
Alternatively (I think) if you don't like indexes then some kind of hash table (or whatever they are called in java).
 
« Last Edit: May 9th, 2004, 10:24pm by kevinP »  

Kevin Pfeiffer
narain


Re: how to dynamically acess an object variable?
« Reply #4 on: May 10th, 2004, 7:52am »

"array indexes (hmm... 'indices')"
Yep, indices.
 
Kevin's right, accessing variables by dynamic strings as names IS a dark and murky hole. It's called reflection, but don't go there.
 
There are messier issues involved here than meet the eye... Look at it from the language point of view: you can't be sure if a variable of that name exists; even if it does, you can't be sure (from outside) what type it is; different variables will have different types, so you can't have a single procedure for handling the result you get back; and so on.
 
(It's much easier in Javascript, btw, because JS isn't strongly typed, AND provides hash tables for free )
 
I can think of two alternatives:
 
As long as you know what the possible names are, you can make a single function with a switch statement, that basically checks what input it gets and returns the corresponding variable value.
 
Or you can just fold all variables into an array, like Kevin said. But then, accessing variables by numeric index is a maintenance nightmare. (It's like coding in machine language ) You COULD put them into a hash, but we're probably getting into more work than it's worth by now.
 
lunetta

7200475272004752 WWW Email
Re: how to dynamically acess an object variable?
« Reply #5 on: May 10th, 2004, 3:56pm »

Why I always fall into dark holes of programming? Can't I just stay in the light?
Thanks for all responses, I managed a workaround for my piece of code...
 
narain


Re: how to dynamically acess an object variable?
« Reply #6 on: May 10th, 2004, 4:04pm »

Yeah... It takes a little while to get a feel for which way you can go, and which way darkness lies.
 
mflux

fluxhavoc WWW
Re: how to dynamically acess an object variable?
« Reply #7 on: May 11th, 2004, 10:55am »

-_-;;;;;
ah... I seem to be so confused and helpless lately. ignore my post above
 
Pages: 1 

« Previous topic | Next topic »