Solved

Dynamically declaring Variables

Posted on 2006-07-12
9
179 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:MayoorPatel
  • 3
  • 3
  • 3
9 Comments
 
LVL 7

Expert Comment

by:SimonBlake
ID: 17091161
Why are you using an array to do this?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 17091176
Hi MayoorPatel,

This is what arrays are for:

Dim Hello(10) As Integer

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

Tim Cottee
0
 
LVL 7

Expert Comment

by:SimonBlake
ID: 17091182
why are you not using an array -  that should be.
0
 
LVL 1

Author Comment

by:MayoorPatel
ID: 17091205
I dont want an array of values though. I just want 5 variables declared as Strings in a tidy loop.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 43

Expert Comment

by:TimCottee
ID: 17091221
MayoorPatel,

The simple answer then is that you cannot.

Tim
0
 
LVL 1

Author Comment

by:MayoorPatel
ID: 17091233
So are you all telling me that if I wanted 50 variables

Hello1
.
.
.
.
Hello50

I'd have to declare them ALL individually?
0
 
LVL 7

Accepted Solution

by:
SimonBlake earned 500 total points
ID: 17091236
Sorry the Dim command does not accept parameters - arrays (single, hashtables etc) are the only way to do this

S.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 17091265
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
 
LVL 1

Author Comment

by:MayoorPatel
ID: 17091277
Hmmmm ok cheers guys, will give it some more thought. Thanks for the prompt replies!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iOS retina Display, HTML Email Signature 5 491
Re-position the objects 7 95
Adjust the position 3 45
Download a website to hdd 2 48
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now