(dumb) Question about naming variables

N

netloss

I have a question so basic that I suspect everyone takes it for
granted, as i cannot google the answer. Anyone here can probably answer
it!

I want to create a bunch of variable at runtime, but I don't know
beforehand how many. When the program has calculated the number of
variables (N) that I need, I pass N to a loop. I then want the loop to
generate N new variables. I want it to name each variable in turn, so
I'll end up with variable named something like this:

var1
var2
var3

etc.

How can I create a variable name that concatenates a standard string
(Var in this case) with an integer-converted-to-a-string?

Thanks!

-fool
 
F

Fool

Thanks! You are probably right, I should be using some kind of
collection in place of individual variables. I will probably use an
arraylist for now.

However, let's assume that I use a hashtable instead. In that case I
need to provide a key value for each item added to the hashtable. I
believe this leads back to my original question: How would I generate
a unique key for each hashtable entry? If I want my keys to be a
combination of some word (say, Item) and a number, I'd try something
like this:

dim keyvalue as string
dim counter as integer

keyvalue = "Item" & counter

dim HT as hashtable

HT.add(someitem, keyvalue)

But instead of adding someitem to the hashtable with a key value of
"Item0", I imagine I'll get an error, or it will assign a key value of
"keyvalue". I am probably not very clear, but I hope you see what I'm
asking....

Thanks again!
 
C

Cor Ligthert

Hi,

When you have to create in your program a key. Than there is in my opinion
no need for a key. You just can use an index as with the arraylist.

When you have a key, than that comes from outside your program and can you
only check if it already exist and take the steps what to do with it on that
what it is. That is not to describe in an abstract way.

Just my thought,

Cor
 
H

Herfried K. Wagner [MVP]

netloss said:
I want to create a bunch of variable at runtime, but I don't know
beforehand how many. When the program has calculated the number of
variables (N) that I need, I pass N to a loop. I then want the loop to
generate N new variables. I want it to name each variable in turn, so
I'll end up with variable named something like this:

\\\
Dim astr(n - 1) As String
For i = 0 To n - 1
astr(i) = ...
Next i
///
 
N

netloss

Herfried, just so I understand your code (I am pretty new at this):

You seem to be saying (lke Cor) that I should just declare an array and
populate it, instead of trying to declare names?
 
H

Herfried K. Wagner [MVP]

netloss said:
Herfried, just so I understand your code (I am pretty new at this):

You seem to be saying (lke Cor) that I should just declare an array and
populate it, instead of trying to declare names?

Yes, if possible that's always a good idea.
 

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