Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dynamically declaring Variables

Posted on 2006-07-12
9
Medium Priority
?
184 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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…
Suggested Courses

971 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