We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Accessing instance name from within class
Page Index Toggle Pages: 1
Accessing instance name from within class (Read 248 times)
Accessing instance name from within class
Jan 12th, 2009, 3:49pm
 
Hi all,

I've got a question.

If you have something like this:

 attractor1 = new Attractor();
 attractor2 = new Attractor();
 attractor3 = new Attractor();

with all the rest of the class stuff, can you see in your attractor class which variable you are assigned to? I need to know for each instance of Attractor whether it's attractor1, attractor2, or attractor3.

The way i'm doing it now is, I create a variable inside the attractor class, and I modify it directly after creation, like this:

 attractor1 = new Attractor();
 attractor1.number = 1;
 attractor2 = new Attractor();
 attractor2.number = 2;
 attractor3 = new Attractor();
 attractor3.number = 3

But I think this is not really the proper way to do it, so is there any other way?
Re: Accessing instance name from within class
Reply #1 - Jan 12th, 2009, 4:10pm
 
To be able to answer, we need to know what you plan to do with this information...

Somehow, the this special variable might be what you look for, as it points to the current object. It won't give you the name of the variable, which is not possible, because several variables can have a reference to the same object.

There is no instance name, only an instance ID.

Note that you can make a constructor of your class to accept a number or a name.
Re: Accessing instance name from within class
Reply #2 - Jan 12th, 2009, 4:23pm
 
you mean like this?

attractor1 = new Attractor(1);
attractor2 = new Attractor(2);
attractor3 = new Attractor(3);

In each Attractor class, I want to send an OSC message over to Pd, while being able to distinguish which message came from which object...
Page Index Toggle Pages: 1