Sorry if this is a stupid question I am a Java newbie! What I want to
know is that is it possible to somehow pass a variable name (a string)
to a method as an argument and then use that as a variable name?
I've had a think about it and can't think of a way to do it so I'm not
sure it can even be done in Java?
Thanks
Steve
James McGill - 02 Mar 2006 18:22 GMT
The concept you're looking for is the "Reflection" API, and you don't
want to go anywhere near it as a newbie. Even with this, I don't think
you can do what you described, exactly.
Rethink your approach to your problem.
Oliver Wong - 02 Mar 2006 18:35 GMT
> Sorry if this is a stupid question I am a Java newbie! What I want to
> know is that is it possible to somehow pass a variable name (a string)
> to a method as an argument and then use that as a variable name?
>
> I've had a think about it and can't think of a way to do it so I'm not
> sure it can even be done in Java?
It can't be done "directly" in the same way that, say, PHP might be able
to do it.
You can fake this sort of behaviour by using a HashMap though.
- Oliver
Gordon Beaton - 02 Mar 2006 18:42 GMT
> Sorry if this is a stupid question I am a Java newbie! What I want
> to know is that is it possible to somehow pass a variable name (a
> string) to a method as an argument and then use that as a variable
> name?
Data structures like HashmMap or Hashtable do what you are asking by
mapping keys (such as Strings) to arbitrary values.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
steve_marjoribanks@hotmail.com - 02 Mar 2006 19:55 GMT
Ok, thanks people, I thought it might be like that. I've changed my
code a bit and split the method into two separate methods and used
'return' to return the int back to the variable name as opposed to
passing the name to the int. I think I'm slowly getting my head round
these things!
Thanks anyhow!
Steve
Hendrik Maryns - 02 Mar 2006 20:42 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message
steve_marjoribanks@hotmail.com uitte de volgende tekst op 03/02/2006
07:12 PM:
> Sorry if this is a stupid question I am a Java newbie! What I want to
> know is that is it possible to somehow pass a variable name (a string)
> to a method as an argument and then use that as a variable name?
>
> I've had a think about it and can't think of a way to do it so I'm not
> sure it can even be done in Java?
Sounds like you are in need of the Command pattern?
H.

Signature
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org