Results 1 to 5 of 5

Thread: Array Troubles

  1. #1
    Klicnik2001
    Guest

    Exclamation

    Just a basic array question that doesn't seem to work for me.
    My code:

    Dim tmp(1 To 1000) As Long
    Car1End = timer
    car1Lap = Val(txtCarLap1.Text)
    txtCarLap1.Text = car1Lap + 1
    tmp(car1Lap) = Car1End


    What I'm trying to do is store each lap in a array, and then find the average. Car1End is a integer, and so is car1Lap. Now, I get a "Subscript out of range" error. Can YOU help?

    Thanks.

  2. #2
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    Try this .....

    Code:
    Dim tmp(1 To 1000) As Long 
    Car1End = timer 
    txtCarLap1.Text = car1Lap + 1 
    car1Lap = Val(txtCarLap1.Text) 
    tmp(car1Lap) = Car1End
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  3. #3
    Guest

    Smile YEAH!

    THANKS!

    That worked...
    I'm not sure why though..

    Can you possible explain what happened?

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    You needed an integer value for the index of the array, the Val() function converts say the character "1" to the number 1. That's why it works now
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    The Subscript out of range error occurs when you try to access an element in an array which does not exist, and in the code you provided the carLap1 variable was not being incremented the first time the code is ran because you were retrieving it's value from an empty textbox which filled the variable with 0, and considering your array starts at element one it was looking for a value that doesn't exist.

    This code would also work...

    Code:
    Dim tmp(0 To 999) As Long 'still 1000 elements but the first is 0
    Car1End = timer 
    car1Lap = Val(txtCarLap1.Text)  ' will return 0 the first time code is ran
    txtCarLap1.Text = car1Lap + 1 
    tmp(car1Lap) = Car1End
    Hope that makes things clearer.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

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