Results 1 to 2 of 2

Thread: Index is ouside the bounds of the array

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    14

    Index is ouside the bounds of the array

    Hi Guys,

    Another error and I'm not sure where it is comming from:

    Code:
    Structure structHours
        Dim StdHours As Integer
        Dim OvHours As Integer
        Dim TotHours As Integer
    End Structure
    
    
    Dim arHours(i) As structHours
    
     Private Sub CalculateHoursSub()
    
    
            'Defining Local Subroutine variables
            Dim intOverTimeHours As Integer                 'Holds overtime hours during this subroutine
            Dim intStandardHours As Integer                 'Holds standard hours during this subroutine
    
            'This subroutine takes the entered number of hours, calculates the no of standard and over time
            'hours, puts them into arrays, and updates variables for the totals of these hours for use in
            'the frmEmployeePayeDetails and frmCalculatedMonthlyPaye.
    
            'Adds 1 to the NoOfDaysCounter, makes the UndoLastEntry button visiable after the 
            'first value has been entered and reads the hours entered from the text box into variable.
    
            intNoOfDaysCounter = intNoOfDaysCounter + 1
            Me.UndoButton.Enabled = True
            intEnteredHours = CInt(Me.NoOfHoursTextBox.Text)
    
            'This if statement sets the variables for the amount standard and over time hours
            If intEnteredHours > 8 Then
                intStandardHours = 8
                intOverTimeHours = (intEnteredHours - intStandardHours)
    
            Else
                intStandardHours = intEnteredHours
                intOverTimeHours = 0
            End If
    
    
            'Adds the values above to the listview 
    
            Dim LVItems As New ListViewItem
            LVItems.Text = CStr(intStandardHours)
            LVItems.SubItems.Add(CStr(intOverTimeHours))
            LVItems.SubItems.Add(CStr(intEnteredHours))
            Me.EnteredHoursListView.Items.Add(LVItems)
    
            'Puts the hours variables into an array
    
            arHours(nIndexItems).StdHours = intStandardHours
            arHours(nIndexItems).OvHours = intOverTimeHours
            arHours(nIndexItems).TotHours = intEnteredHours
    
    
            'Adds standard and overtime variables for this entry to the running total of hours
    
            intTotalHours = intTotalHours + intEnteredHours
            intTotalStandardHours = intTotalStandardHours + intStandardHours
            intTotalOvertTimeHours = intTotalOvertTimeHours + intOverTimeHours
    
    
            'Resets hours entry box
    
            Me.NoOfHoursTextBox.Clear()
            Me.NoOfHoursTextBox.Text = "0"
    
    
            If intNoOfDaysCounter <> intNoOfDays + 1 Then
                'Adds one to the nhours variable used to track the current line of the array
                nIndexItems = nIndexItems + 1
                intUndo = nIndexItems
                'Updates the day text box and sets focus on EnteredHoursTextBox
                Me.DayLabel.Text = CStr(intNoOfDaysCounter)
                Me.NoOfHoursTextBox.Focus()
    
            Else
                MsgBox("You have enter the required number of days. Please close the form to continue")
                Me.NoOfHoursTextBox.Enabled = False
                Me.AcceptButton.Enabled = False
    
            End If
    
        End Sub

    When I push the button the first time this works fine. When I do it a second time I get the error:

    Index is ouside the bounds of the array

    Hovering over where the code breaks down:
    Code:
     arHours(nIndexItems).StdHours = intStandardHours
    shows that the index value is currently 1. Howevering over i in :
    Code:
    Dim arHours(i) As structHours
    Shows i to have a value of 30. Where is this errors comming from?

  2. #2
    Lively Member eatmycode's Avatar
    Join Date
    Mar 2010
    Location
    Kingston upon Hull
    Posts
    74

    Re: Index is ouside the bounds of the array

    Can you provide some more details, like how are you initializing your array? You declare it with an indefinite number (i). You usually initialize an array with a definite number of items, or reset it with the ReDim statement. Based on your info you are initializing the array with 30 structHours objects. And then if you could show how you are iterating through the array would also be helpful.

    Why can't you use a typed collection? Much simpler to work with:

    vb Code:
    1. ' Create your structure.
    2. Public Structure StructHours
    3.     Public myHours1 As Integer
    4.     Public overtime As Integer
    5.     Public salary As Double
    6. End Structure
    7.  
    8. ' Then create the collection, such as a Dictionary.
    9.         Dim coll1 As New Collections.Generic.Dictionary(Of Integer, StructHours)
    10.         Dim hours As New StructHours
    11.         hours.myHours1 = 10
    12.         hours.overtime = 20
    13.         hours.salary = 100.233
    14.         coll1.Add(1, hours)

    And to extract the values:
    vb Code:
    1. For Each kvp As KeyValuePair(Of Integer, StructHours) In coll1
    2.             ' Do other work with kvp collection...
    3.             If kvp.Key = 12 Then
    4.  
    5.             End If
    6.         Next

    You would normally just access the collection using your Key value. And this Key value again can be any object, String, Integer, etc.

    This is a much better way of organising an array. It's type safe and will improve performance and reduce the amount of stress when things go wrong.
    Last edited by eatmycode; Mar 11th, 2010 at 07:58 PM.
    Technik ... Kniff, die Welt so einzurichten, dass wir sie nicht erleben mussen

    Max Frisch

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