use the value of one variable as a new variable's name?

M

Mark

I know I should know this, but can't figure it out.
Please help.

In a function I will be passing a number (int). Let's
call this variable 'order'
I then create a variable that appends this
variable 'order' to a set string. And that's easy
enough --

variable = "item" & order

The problem is that I now need that concatenated string --
item1 -- to be the name of a (3rd) variable I use.

In other words the function will be written to do
different things depending on the item, so logic varies
among item1, item2, and so on.
Once I derive the 'name' of the variable (item1, or
item2, etc.) how do I then use that derivation as a
variable name?

Thanks for *any* help -- (e-mail address removed)
 
N

Neil

Some interpreted languages allow you to do this (CList,
Rexx, etc...) but it's pretty rare an higher leve
languages which almost always expect you to use arrays or
indexing instead.

Instead of defining 3 variables... Item1, item2, item3 you
would define an array of 3 of the variable Item (3).

Then instead of developing the name by concatenating
strings ("Item" & Order) you would use the name you had
and subscript into it...
Item (Order) will refer to the Orderth instance of item.

Now... if you trying to do something tricksy, like
allowing users to define their own variable names, this
won't work and you've got more difficulties ahead than a
simple syntax question can solve ... but it should be good
for everything else.
 
M

Mark

Thanks, that approach should work....
-----Original Message-----
Some interpreted languages allow you to do this (CList,
Rexx, etc...) but it's pretty rare an higher leve
languages which almost always expect you to use arrays or
indexing instead.

Instead of defining 3 variables... Item1, item2, item3 you
would define an array of 3 of the variable Item (3).

Then instead of developing the name by concatenating
strings ("Item" & Order) you would use the name you had
and subscript into it...
Item (Order) will refer to the Orderth instance of item.

Now... if you trying to do something tricksy, like
allowing users to define their own variable names, this
won't work and you've got more difficulties ahead than a
simple syntax question can solve ... but it should be good
for everything else.


.
 

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

Top