Results 1 to 11 of 11

Thread: [RESOLVED] [2005] Declaring Variables in a loop.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    23

    Resolved [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:
    1. For X= 1 To Test
    2.  Dim Result(Test) as String
    3. Result(Test) = (Insert  some unimportant code here)
    4. 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

  2. #2
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    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:
    1. ' Remember the number represents the upper limit of the array in VB6
    2. Dim Result(Test -1 ) as String  
    3. For X= 1 To Test
    4.  
    5.    Dim temp1 as String
    6.    ' do something with temp1
    7.    Result(Test) = (Insert  some unimportant code here)
    8.  
    9. 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.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    23

    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:
    1. Dim DiceNum As Integer
    2.         Dim X As Integer
    3.         Dim DieFaces As Integer
    4.         DiceNum = Val(InputBox$("Enter the number of Dice"))
    5.         DieFaces = Val(InputBox$("Enter the number of sides"))
    6.  
    7.         Dim Result(DiceNum - 1) As String
    8.         For X = 1 To DiceNum
    9.             Result(DiceNum - 1) = CInt(Int((DieFaces * Rnd()) + 1))
    10.             MsgBox(Result(DiceNum - 1))
    11.         Next X

  4. #4
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: [2005] Declaring Variables in a loop.

    I'd say it should be

    VB Code:
    1. Dim DiceNum As Integer
    2.         Dim X As Integer
    3.         Dim DieFaces As Integer
    4.         DiceNum = Val(InputBox$("Enter the number of Dice"))
    5.         DieFaces = Val(InputBox$("Enter the number of sides"))
    6.  
    7.         Dim Result(DiceNum - 1) As String
    8.         For X = 1 To DiceNum
    9.             ' In your code you use DiceNum-1 whcih is wrong
    10.             ' as it will always be the same.
    11.             Result(X) = CInt(Int((DieFaces * Rnd()) + 1))
    12.             MsgBox(Result(X))
    13.         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.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    23

    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-

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    23

    Re: [2005] Declaring Variables in a loop.

    VB Code:
    1. Dim DiceNum As Integer
    2.         Dim X As Integer
    3.         Dim DieFaces As Integer
    4.         Dim Nums As String
    5.         DiceNum = Val(InputBox$("Enter the number of Dice"))
    6.         DieFaces = Val(InputBox$("Enter the number of sides"))
    7.  
    8.         Dim Result(DiceNum - 1) As String
    9.         For X = 1 To DiceNum
    10.             Result(X) = CInt(Int((DieFaces * Rnd()) + 1))
    11.             Nums = Str$(Result(X)) & "," & Nums
    12.         Next X
    13.  
    14.         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-

  8. #8
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Declaring Variables in a loop.

    Is this VB6 or VB.NET code? Looks like VB6 to me...

  9. #9
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Declaring Variables in a loop.

    Using more .NET-like code...
    VB Code:
    1. Dim DiceNum As Integer = CInt(InputBox("Enter the number of Dice"))
    2.         Dim DieFaces As Integer = CInt(InputBox("Enter the number of sides"))
    3.         Dim Result As String = ""
    4.         Dim MyRandom As New Random
    5.         For X As Integer = 0 To DiceNum - 1
    6.             Result &= MyRandom.Next(1, DieFaces + 1).ToString & " "
    7.         Next
    8.         MessageBox.Show("You got:" & Result)

  10. #10
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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:
    1. ' Remember the number represents the upper limit of the array in VB6
    2. Dim Result(Test -1 ) as String  
    3. For X= 1 To Test
    4.  
    5.    Dim temp1 as String
    6.    ' do something with temp1
    7.    Result(Test) = (Insert  some unimportant code here)
    8.  
    9. 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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  11. #11
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: [RESOLVED] [2005] Declaring Variables in a loop.

    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
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width