Solved

Dynamically declaring Variables

Posted on 2006-07-12
9
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
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 …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

710 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