[RESOLVED] [2005] Declaring Variables in a loop.
Is it possible to declare variables within a loop.
For example if I wanted say 1-20 differnt variables defined according to what a user wanted can you declare a variable using the incremented number itself?
So like
VB Code:
For X= 1 To Test
Dim Result(Test) as String
Result(Test) = (Insert some unimportant code here)
Next X
Does that work or how would I need to change it to work like that? (if its even possible) Or am I stuck using obsurbly long if statements to determine all that?
DAve
Re: [2005] Declaring Variables in a loop.
Based on teh code you've postd I can something wrong here.
Becase you know the value of Test prior to entering the loop you can define the array before starting the loop.
So the code should be:
VB Code:
' Remember the number represents the upper limit of the array in VB6
Dim Result(Test -1 ) as String
For X= 1 To Test
Dim temp1 as String
' do something with temp1
Result(Test) = (Insert some unimportant code here)
Next X
As you can see I can define a variable within the loop though it would be better to define it outside the loop.
Re: [2005] Declaring Variables in a loop.
Ok so if I wanted to generate a number of random numbers (like dice) and display them I would put:
VB Code:
Dim DiceNum As Integer
Dim X As Integer
Dim DieFaces As Integer
DiceNum = Val(InputBox$("Enter the number of Dice"))
DieFaces = Val(InputBox$("Enter the number of sides"))
Dim Result(DiceNum - 1) As String
For X = 1 To DiceNum
Result(DiceNum - 1) = CInt(Int((DieFaces * Rnd()) + 1))
MsgBox(Result(DiceNum - 1))
Next X
Re: [2005] Declaring Variables in a loop.
I'd say it should be
VB Code:
Dim DiceNum As Integer
Dim X As Integer
Dim DieFaces As Integer
DiceNum = Val(InputBox$("Enter the number of Dice"))
DieFaces = Val(InputBox$("Enter the number of sides"))
Dim Result(DiceNum - 1) As String
For X = 1 To DiceNum
' In your code you use DiceNum-1 whcih is wrong
' as it will always be the same.
Result(X) = CInt(Int((DieFaces * Rnd()) + 1))
MsgBox(Result(X))
Next X
On another note you're using VB6 style functions, you should use MessageBox.Show instead of MsgBox and Integer.Parse rather than Cint.
Re: [2005] Declaring Variables in a loop.
Ok thanks. Out of curiosity is there a way for me to modify that code so that it will post all the results within one message box?
DAve-
Re: [2005] Declaring Variables in a loop.
You'd need to put all the numbers in a single string and show that in a message box at the end of the loop. You'd start with an empty string and simply append each number as it was generated. Note that you'll need to put some sort of separator between the numbers.
Re: [2005] Declaring Variables in a loop.
VB Code:
Dim DiceNum As Integer
Dim X As Integer
Dim DieFaces As Integer
Dim Nums As String
DiceNum = Val(InputBox$("Enter the number of Dice"))
DieFaces = Val(InputBox$("Enter the number of sides"))
Dim Result(DiceNum - 1) As String
For X = 1 To DiceNum
Result(X) = CInt(Int((DieFaces * Rnd()) + 1))
Nums = Str$(Result(X)) & "," & Nums
Next X
MsgBox("You got:" & Nums)
That doesnt work for me? Any Idea why? It says its just the Random number Generator part but VB has some poor quality help.
DAve-
Re: [2005] Declaring Variables in a loop.
Is this VB6 or VB.NET code? Looks like VB6 to me...
Re: [2005] Declaring Variables in a loop.
Using more .NET-like code...
VB Code:
Dim DiceNum As Integer = CInt(InputBox("Enter the number of Dice"))
Dim DieFaces As Integer = CInt(InputBox("Enter the number of sides"))
Dim Result As String = ""
Dim MyRandom As New Random
For X As Integer = 0 To DiceNum - 1
Result &= MyRandom.Next(1, DieFaces + 1).ToString & " "
Next
MessageBox.Show("You got:" & Result)
Re: [2005] Declaring Variables in a loop.
Quote:
Originally Posted by Mr.No
Based on teh code you've postd I can something wrong here.
Becase you know the value of Test prior to entering the loop you can define the array before starting the loop.
So the code should be:
VB Code:
' Remember the number represents the upper limit of the array in VB6
Dim Result(Test -1 ) as String
For X= 1 To Test
Dim temp1 as String
' do something with temp1
Result(Test) = (Insert some unimportant code here)
Next X
As you can see I can define a variable within the loop though it would be better to define it outside the loop.
Won't that throw an exception on the last element in the array?
Bill
Re: [RESOLVED] [2005] Declaring Variables in a loop.
Quote:
Won't that throw an exception on the last element in the array?
conipto - Yep it would, I didn't notice that the loop started with X = 1 rather than 0 :D