Declaring variables in a For Next loop

  • Thread starter Thread starter wandoledzep
  • Start date Start date
W

wandoledzep

I'm using Visual Studio .NET 2002
I remember there was a way to do this in C, so there should be a way of
doing it in .NET:

Instead of:

Dim bolSquare1 as Boolean
Dim bolSquare2 as Boolean
Dim bolSquare3 as Boolean
|
|
|
Dim bolSquare9 as Boolean

Is there some way of doing it like this:

Dim i as Integer
For i = 1 to 9
Dim bolSquare(i) as Boolean
Next

Any help is MUCH appreciated.

Peace,
LedZep
 
I'm using Visual Studio .NET 2002

Upgrade. You are now two versions behind.

For i as Integer = 0 to 9
'whatever
Next

Works as of .NET 1.1 (VS2003).

David
 
Upgrade ::: You missed the correct interpretation of his question :-)

he wants to dynamicly declare ( create variabels ) in a loop


Michel Posseth [MCP]
 
you can dynamicly create an array of anny type like this

Dim items As Integer = 10
Dim bolSquare(items) As Boolean


MsgBox(bolSquare(1))
MsgBox(bolSquare(2))
MsgBox(bolSquare(3))

etc etc

so in items you can put the required numeber of items

isn`t that not much easier and cleaner code ?

regards

Michel Posseth [MCP]
 
M. Posseth said:
Upgrade ::: You missed the correct interpretation of his question :-)

he wants to dynamicly declare ( create variabels ) in a loop
Oops.

David
 
But I don't want to create an array--I want to create ten different
Boolean variables (ex. bol1, bol2, bol3, bol4, bol5, bol6, etc.) using
a For Next loop. I want to figure this out because if I have to declare
100 different variables all the same except with different numbers, why
can't I use some kind of loop?


M. Posseth said:
you can dynamicly create an array of anny type like this

Dim items As Integer = 10
Dim bolSquare(items) As Boolean


MsgBox(bolSquare(1))
MsgBox(bolSquare(2))
MsgBox(bolSquare(3))

etc etc

so in items you can put the required numeber of items

isn`t that not much easier and cleaner code ?

regards

Michel Posseth [MCP]



I'm using Visual Studio .NET 2002
I remember there was a way to do this in C, so there should be a way of
doing it in .NET:

Instead of:

Dim bolSquare1 as Boolean
Dim bolSquare2 as Boolean
Dim bolSquare3 as Boolean
|
|
|
Dim bolSquare9 as Boolean

Is there some way of doing it like this:

Dim i as Integer
For i = 1 to 9
Dim bolSquare(i) as Boolean
Next

Any help is MUCH appreciated.

Peace,
LedZep
 
I dont think you can name a variable at run time, I think you need it
predefined in the code.

That said, why do you want to use 100 different variables instead of an
array with 100 members? An array is usually by far the cleanest way to do
this, what is pecial about this situation that you want 100 different
variables floating about?


But I don't want to create an array--I want to create ten different
Boolean variables (ex. bol1, bol2, bol3, bol4, bol5, bol6, etc.) using
a For Next loop. I want to figure this out because if I have to declare
100 different variables all the same except with different numbers, why
can't I use some kind of loop?


M. Posseth said:
you can dynamicly create an array of anny type like this

Dim items As Integer = 10
Dim bolSquare(items) As Boolean


MsgBox(bolSquare(1))
MsgBox(bolSquare(2))
MsgBox(bolSquare(3))

etc etc

so in items you can put the required numeber of items

isn`t that not much easier and cleaner code ?

regards

Michel Posseth [MCP]



I'm using Visual Studio .NET 2002
I remember there was a way to do this in C, so there should be a way of
doing it in .NET:

Instead of:

Dim bolSquare1 as Boolean
Dim bolSquare2 as Boolean
Dim bolSquare3 as Boolean
|
|
|
Dim bolSquare9 as Boolean

Is there some way of doing it like this:

Dim i as Integer
For i = 1 to 9
Dim bolSquare(i) as Boolean
Next

Any help is MUCH appreciated.

Peace,
LedZep
 
i understand that

afaik : this is not possible the way you described it , i also don`t see
the point of not using a array in this case ( or a named collection , if you
prefer that )


regards
\
Michel Posseth [MCP]

But I don't want to create an array--I want to create ten different
Boolean variables (ex. bol1, bol2, bol3, bol4, bol5, bol6, etc.) using
a For Next loop. I want to figure this out because if I have to declare
100 different variables all the same except with different numbers, why
can't I use some kind of loop?


M. Posseth said:
you can dynamicly create an array of anny type like this

Dim items As Integer = 10
Dim bolSquare(items) As Boolean


MsgBox(bolSquare(1))
MsgBox(bolSquare(2))
MsgBox(bolSquare(3))

etc etc

so in items you can put the required numeber of items

isn`t that not much easier and cleaner code ?

regards

Michel Posseth [MCP]



I'm using Visual Studio .NET 2002
I remember there was a way to do this in C, so there should be a way of
doing it in .NET:

Instead of:

Dim bolSquare1 as Boolean
Dim bolSquare2 as Boolean
Dim bolSquare3 as Boolean
|
|
|
Dim bolSquare9 as Boolean

Is there some way of doing it like this:

Dim i as Integer
For i = 1 to 9
Dim bolSquare(i) as Boolean
Next

Any help is MUCH appreciated.

Peace,
LedZep
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top