Solved

Dynamically declaring Variables

Posted on 2006-07-12
9
178 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

744 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

13 Experts available now in Live!

Get 1:1 Help Now