Dynamically declaring Variables

Hi there I want to do something as follows in VB.NET, however its obviously not possible after testing it out.

Dim i as integer
for i = 0 to 10
   Dim "Hello" & i as String
Next

Can anyone help show me how to declare variables in a loop?

I dont want to have to do

Dim Hello1 as integer
Dim Hello2 as integer
Dim Hello3 as integer
Dim Hello4 as integer
.
.
.
.

as its untidy.

Thanks
LVL 1
MayoorPatelAsked:
Who is Participating?
 
SimonBlakeConnect With a Mentor Commented:
Sorry the Dim command does not accept parameters - arrays (single, hashtables etc) are the only way to do this

S.
0
 
SimonBlakeCommented:
Why are you using an array to do this?
0
 
TimCotteeHead of Software ServicesCommented:
Hi MayoorPatel,

This is what arrays are for:

Dim Hello(10) As Integer

Hello(0) = 50
Hello(1) = 70
etc

Tim Cottee
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
SimonBlakeCommented:
why are you not using an array -  that should be.
0
 
MayoorPatelAuthor Commented:
I dont want an array of values though. I just want 5 variables declared as Strings in a tidy loop.
0
 
TimCotteeHead of Software ServicesCommented:
MayoorPatel,

The simple answer then is that you cannot.

Tim
0
 
MayoorPatelAuthor Commented:
So are you all telling me that if I wanted 50 variables

Hello1
.
.
.
.
Hello50

I'd have to declare them ALL individually?
0
 
TimCotteeHead of Software ServicesCommented:
MayoorPatel,

I am afraid so, there is no capability in VB, VB.NET etc to do this kind of thing. That is why arrays, arrylists, collections and other datatypes are provided. Foxpro used to have something like this (macro expansion) but even then you would have to declare the variables explicitly. You could however refer to them in a loop in this kind of way.

Tim
0
 
MayoorPatelAuthor Commented:
Hmmmm ok cheers guys, will give it some more thought. Thanks for the prompt replies!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.