Results 1 to 3 of 3

Thread: ReDim Array - "Subscript out of range"

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    47
    I am working on a homework assignment, my program is erroring in the last step.

    During the step through all looks good, then when it comes to my print statement I get the error, subscript out of range.

    This returns on an array value that is a decimal. I've read that "Subscripts in ReDim statements whose values are not whole numbers are rounded to the nearest whole number - subscripts outside this range return the error "subscript out of range".

    Is there a way around this???

    UPDATE:
    I've multiplied the decimal to create a whole number, same error. This is not why the error is happening!

    when I step through - it all looks good until I get to
    DisplayData print statement.

    Private Sub cmdDisplay_Click()
    Dim numTeams As Integer
    numTeams = countNumTeams(App.Path & "\ale.txt")
    ReDim team(1 To numTeams) As String
    ReDim wins(1 To numTeams) As Integer
    ReDim lost(1 To numTeams) As Integer
    ReDim pct(1 To numTeams) As String
    Call readFile(team(), wins(), lost(), pct(), numTeams)
    Call sortData(team(), wins(), lost(), pct(), numTeams)
    Call DisplayData(team(), wins(), lost(), numTeams)
    End Sub

    Private Sub cmdExit_Click()
    End
    End Sub

    Private Function countNumTeams(filespec As String) As Integer
    Dim team As String, wins As Integer, lost As Integer
    Dim n As Integer
    n = 0
    Open filespec For Input As #1
    Do While Not EOF(1)
    Input #1, team, wins, lost
    n = n + 1
    Loop
    Close #1
    countNumTeams = n
    End Function

    Private Sub readFile(team() As String, wins() As Integer, _
    lost() As Integer, pct() As String, numTeams As Integer)
    Dim index As Integer, percent As Single
    Open App.Path & "\ale.txt" For Input As #1
    For index = 1 To numTeams
    Input #1, team(index), wins(index), lost(index)
    pct(index) = FormatNumber(wins(index) / (lost(index) + wins(index)), 3)
    Next index
    Close #1
    End Sub

    Private Sub DisplayData(team() As String, wins() As Integer, _
    lost() As Integer, numTeams As Integer)
    Dim index As Integer, pct() As String
    Dim fmt As String
    picDisplay.Cls
    For index = 1 To numTeams
    fmt = "@@@@@@"
    picDisplay.Print team(index), Format(wins(index), fmt), _
    Format(lost(index), fmt), Format(pct(index), fmt)
    Next index
    End Sub

    Private Sub swapData(team() As String, wins() As Integer, _
    lost() As Integer, pct() As String, index As Integer)
    Dim ttemp As String, wtemp As Integer, ltemp As Integer
    ttemp = team(index)
    team(index) = team(index + 1)
    team(index + 1) = ttemp
    wtemp = wins(index)
    wins(index) = wins(index + 1)
    wins(index + 1) = wtemp
    ltemp = lost(index)
    lost(index) = lost(index + 1)
    lost(index + 1) = ltemp
    ptemp = pct(index)
    pct(index) = pct(index + 1)
    pct(index + 1) = ptemp
    End Sub

    Private Sub sortData(team() As String, wins() As Integer, _
    lost() As Integer, pct() As String, numTeams As Integer)
    Dim passnum As Integer, index As Integer
    'sort by pct
    For passnum = 1 To numTeams - 1
    For index = 1 To numTeams - passnum

    If pct(index) < pct(index + 1) Then
    Call swapData(team(), wins(), lost(), pct(), index)
    End If
    Next index
    Next passnum
    End Sub


    [Edited by koperski on 06-18-2000 at 08:39 AM]

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    NY
    Posts
    497

    Use another variable besides index

    index or Index is a vb kind of variable. Try something else. Initialize your variables.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    47

    FOUND IT!!

    I found my error -
    I didn't pass the array in ReadData - I dimmed it as a string.

    Sometimes I wonder ....

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