-
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.
-
Try this .....
Code:
Dim tmp(1 To 1000) As Long
Car1End = timer
txtCarLap1.Text = car1Lap + 1
car1Lap = Val(txtCarLap1.Text)
tmp(car1Lap) = Car1End
-
YEAH!
THANKS!
That worked...
I'm not sure why though..
Can you possible explain what happened?
-
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
-
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.