Results 1 to 5 of 5

Thread: removing instances from an object array

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Location
    Guadalajara, Jalisco Mexico
    Posts
    9

    Lightbulb

    I'm working on a project that let access people from many places in my country to a Database, this program may work with many conections at a time, at this moment i have a dynamic array of Winsocks, add is very simple... but when one of the connections is "closed by peer" I get the Close event, there i close the socket, i unload it, and when i try to reindex the other sockets i can't, i get a hole in my array, why?

    can anyone help me?

    on the MSDN CD says, try setting a new index, but when i try to assign an index to the object it generates an error, a friend told me "change the object name before unload it" but i don't know if change the name of an object can be possible, it is?

    another idea?

    thanx for any support.

    JJDriVeR

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Instead of trying to ReIndex the Control Array, why not just reuse those Indexes that are leaving holes in the Array? You could write a simple function to get the next available Index for you, i.e.
    Code:
    Function GetNextID(oObjArray As Variant) As Long
        Dim nIndex As Long
        Dim oObj As Variant
        Dim bFound As Boolean
        
        For nIndex = 0 To oObjArray.Count
            bFound = False
            For Each oObj In oObjArray
                If oObj.Index = nIndex Then
                    bFound = True
                    Exit For
                End If
            Next
            If Not bFound Then Exit For
        Next
        GetNextID = nIndex
    End Function
    Example:
    Code:
    Private Sub Command1_Click()
        MsgBox "The next Index is " & GetNextID(wskArray)
    End Sub

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Or you could use a classcollection instead of object array and it will automatically remove the holes when you remove an item
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Location
    Guadalajara, Jalisco Mexico
    Posts
    9

    Thanx in advance

    Thanx for the help I'll use it, and thanx too for the other suggestion i will not use it because i need to redo many code that is implemented based on an array, but for the next time I will try in that new way...

    thanx to both and for any other reply!

    JJDriVeR

  5. #5
    Addicted Member
    Join Date
    Oct 2000
    Location
    Orlando, FL
    Posts
    253
    I have an array of thousands of items and if the hole(s) are
    torwards the end of the array, it takes time to find the missing
    arrayitems with that loop.

    So, I created a seperate array of indexes that keeps track of
    deleted items. (You could also create a comma delimited string as well).

    VB Code:
    1. Private mintControlCount As Integer
    2. Private mintIndexCount as Integer
    3. Private intIndexes() as Integer
    4.  
    5. Private Sub Remove(Index As Integer)
    6. 'You cant unload the original control (In my case it is objItems(0))
    7. If Index <> 0 Then
    8.     'You dont need to add to the indexes if the removed control is
    9.     'the last control
    10.     If mintControlCount - 1 <> Index Then
    11.         'Add a new item to the index array
    12.         Redim Preserve intIndexes(mintIndexCount)
    13.         intIndexes(mintIndexCount) = Index
    14.         'Iterate the index counter
    15.         mintIndexCount = mintIndexCount + 1
    16.     End If
    17.  
    18.     Unload objItems(Index)
    19.     'De-Iterate the control counter
    20.     mintControlCount = mintControlCount - 1
    21.     End If
    22. End If
    23. End Sub
    When you create new controls, check for any items in the Index array first. If there are none then use the top control as the new
    control.
    Always looking for a better and faster way!

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