Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27514

VS 2010 Deleting array element runs FOR loop out of bounds. -Alternatives?

$
0
0
Attachment 95213Hello again, my friends. :)

I have a small problem, and can't wrap my head around any other way do perform the task.

In my main form, I have a timer. This timer does checks on "objects", which is a dynamic array of a class containing the code and Variables for each object.
In these checks, it runs a FOR loop, of bounds 0 to the upperbound of the Array which holds the object, checking if each has fallen below the window.
If it has fallen below, it is to run a Sub Routine that removes that element, and moves all the others down to fill it's place.
The problem is, once the FOR loop has reached the upper bounds of the Object, it may no longer be as long as it once was, and such, crashes as it attempts to check it's bounds.

Example:
code snippet:
Code:

Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        If DrawingNow = True Then ''''''''''''''''''''
            MakeAsDrawing(theLine.GetUpperBound(0))  'This Allows the line to be drawn while creating it.
        End If
        If Not objects(0).hold.Exists = False Then 'Everything that every object does per Cycle here. Runs through all of them.
            For I = 0 To objects.GetUpperBound(0)

                If Not IsNothing(objects(I)) And objects(I).hold.Bounds.Top > Me.Bottom Then 'Make sure it doesn't delete the 0 Element.

                    removeThis(objects(I)) ' SEE: removerThis BELOW
                End If

                objectTest.Text = objects.GetUpperBound(0)
                objects(I).TimerTick() 'Individual tick cycles in here. (Drop rate, etc) (Obs)
                objects(I).Collision() ' Collision here. (Obs)

            Next
        End If


Public Sub removeThis(ByRef Obs As Objective)
        ' SyncLock objects
        Dim index As Integer = Obs.hold.myIndex
        objects(index).hold.Exists = False
        If objects.GetUpperBound(0) > 0 And Not index = 0 Then 'If There is more than one object, and object isn't 0, then...
            Dim arrayOne(0 To index - 1) As Objective ' Create an array from 0 to on less than the index removing
            Dim arrayTwo(0 To objects.GetUpperBound(0) - index) 'Create an array from 0 to the number after the index removing.
            Array.Copy(objects, 0, arrayOne, 0, index - 1) ' Copy the elements from the Original to the temp, one less than removing index.
            Array.Copy(objects, index + 1, arrayTwo, 0, arrayTwo.GetUpperBound(0)) ''Copy the elements from one greater than index to end.
            ' SO: Example
            ' [0][1][2][3][4][5][6][7][8] (index = 3)
            ' [0][1][2][X][4][5][6][7][8]
            ' |arryone|  |--array two--|
            ' [0][1][2][3][4][5][6][7] (resize to one less)
            ' [0][1][2][3][4][5][6][7] (copy arrayone to first half, arraytwo second) (o = Arrayone, t = arraytwo)
            '  o  o  o  t  t  t  t  t
            'Done.
            ReDim objects(objects.GetUpperBound(0) - 1)
            Array.Copy(arrayOne, 0, objects, 0, index - 1)
            Array.Copy(arrayTwo, 0, objects, index, objects.GetUpperBound(0) - index)
            For moveDownIndexValues = index To objects.GetUpperBound(0)
                objects(moveDownIndexValues).hold.myIndex -= 1
            Next
        ElseIf objects.GetUpperBound(0) > 0 And index = 0 Then ' IF there is more than one, and the first one[0] is being removed...
            Dim tempArray(0 To objects.GetUpperBound(0) - 1) 'Create an array that is one less than the size of original
            Array.Copy(objects, 1, tempArray, 0, objects.GetUpperBound(0)) 'Copy original, starting from 1 (second object).
            ReDim objects(objects.GetUpperBound(0) - 1) ' Resize one less
            Array.Copy(tempArray, 0, objects, 0, 1) ' Copy the one element back in.
        ElseIf objects.GetUpperBound(0) = 0 Then
            objects(0).hold.Exists = False

        End If
        For one = 0 To index
            objects(one).hold.myIndex -= 1
        Next
        '
    End Sub

I hope that makes some sense. :)
Thank in advance.
Attached Files

Viewing all articles
Browse latest Browse all 27514

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>