dynamic object referencing

C

Colin Hemmings

Hi there,
I dont seem to be able to find the answer to this anywhere, so I am
wondering if anyone can help me. I am trying to see if there is a way of
referencing a object dynamically by passing it a variable.

For instance say I have an object called 'myObject', which contains
a public method called 'getTotal()'.
I also have a string variable called 'objectName', which contains
the string "myObject".

Is there a way of referencing 'myObject' using the contents of the
string variable. So is there a way to get the following line of code, or
something equivalent, to work:


objectName = "myObject";

objectName.getTotal();

Ive tried casting 'objectName' but that doesnt work, does anyone have
any suggestions?
 
M

Mike Schilling

Colin Hemmings said:
Hi there,
I dont seem to be able to find the answer to this anywhere, so I am
wondering if anyone can help me. I am trying to see if there is a way of
referencing a object dynamically by passing it a variable.

For instance say I have an object called 'myObject', which contains a
public method called 'getTotal()'.
I also have a string variable called 'objectName', which contains the
string "myObject".

Is there a way of referencing 'myObject' using the contents of the
string variable.

No. Local variable names are not known at runtime.
 
G

Gordon Beaton

I dont seem to be able to find the answer to this anywhere, so I am
wondering if anyone can help me. I am trying to see if there is a
way of referencing a object dynamically by passing it a variable.

Your question is asked (and answered) here on a regular basis.
Ive tried casting 'objectName' but that doesnt work, does anyone
have any suggestions?

Casting doesn't do magic. Store your object reference in a HashMap,
using the Strings as keys.

/gordon
 
I

isamura

: Hi there,
: I dont seem to be able to find the answer to this anywhere, so I am
: wondering if anyone can help me. I am trying to see if there is a way of
: referencing a object dynamically by passing it a variable.
:
: For instance say I have an object called 'myObject', which contains
: a public method called 'getTotal()'.
: I also have a string variable called 'objectName', which contains
: the string "myObject".
:
: Is there a way of referencing 'myObject' using the contents of the
: string variable. So is there a way to get the following line of code, or
: something equivalent, to work:
:
:
: objectName = "myObject";
:
: objectName.getTotal();
:
: Ive tried casting 'objectName' but that doesnt work, does anyone have
: any suggestions?
:
I suspect this is not the correct approach regardless of language. A more realistic solution is to
store the actual object, myObject in a Hash ('myObject' -> myObject). Then you can use Hash.get()
with 'myObject' string to get at it.

..k
 
I

isamura

: : Hi there,
: : I dont seem to be able to find the answer to this anywhere, so I am
: : wondering if anyone can help me. I am trying to see if there is a way of
: : referencing a object dynamically by passing it a variable.
: :
: : For instance say I have an object called 'myObject', which contains
: : a public method called 'getTotal()'.
: : I also have a string variable called 'objectName', which contains
: : the string "myObject".
: :
: : Is there a way of referencing 'myObject' using the contents of the
: : string variable. So is there a way to get the following line of code, or
: : something equivalent, to work:
: :
: :
: : objectName = "myObject";
: :
: : objectName.getTotal();
: :
: : Ive tried casting 'objectName' but that doesnt work, does anyone have
: : any suggestions?
: :
: I suspect this is not the correct approach regardless of language. A more realistic solution is to
: store the actual object, myObject in a Hash ('myObject' -> myObject). Then you can use Hash.get()
: with 'myObject' string to get at it.
:
: .k
:
I must have been thinking about Hasheesh...I really mean Map...

..k
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top