Results 1 to 2 of 2

Thread: Index Out Of Range Exception

  1. #1

    Thread Starter
    Hyperactive Member Art W.'s Avatar
    Join Date
    Apr 2002
    Location
    In My Own Little World, But that’s OK because they know me there!
    Posts
    271

    Index Out Of Range Exception

    Hello Everyone:

    I keep getting the error message “Index out of range exception” when I run the following code.


    XX Code:
    1. GN3 = (Loops * 5)
    2.  
    3.         For a = 1 To Loops
    4.  
    5.             For b = 1 To GN3
    6.                 R1 = Seeds1(Counter1)
    7.                 Randomize()
    8.                 R2 = CInt(Int((56 * Rnd(R1)) + 1))
    9.                 Counter1 += 1
    10.  
    11.                 MessageBox.Show(R1 & "   " & Counter1 & "   " & b & "   " & GN3)
    12.  
    13.             Next b
    14.         Next a


    The message box conforms that the last entry in the array has been reached. And that Counter1 and b both start at 1 and go to 2845. And that GN3 is also 2845 all of the time. What am I doing wrong?

    Thanks

    Art W.
    SLEEP: A Totally Inadequate Substation For Caffeine!

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

    Re: Index Out Of Range Exception

    .NET arrays and collections are zero-based, i.e. an array with a Length on N has elements at indexes 0 to (N-1).

    Also, if you're going to use Randomize and Rnd then don't call Randomize over and over. You're supposed to call it once and once only. That said, don't use Randomize and Rnd anyway. This is VB.NET, not VB6, so use the Random class.
    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

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