Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Jul 2, 2001

Remove an Object From a Collection When the Index and Key Are Unknown


This can be done by using the is operator. Here is a simple function demonstrating this method:
 
Public Function RemoveObject(Collection As Collection, Object 
As Object)
Dim vTemp As Object
Dim Index As Integer
    Index = 1
    For Each vTemp In Collection
        If vTemp Is Object Then
            Collection.Remove Index
        Else
            Index = Index + 1
        End If
    Next
End Function

If there is only a single instance in the collecion, you can add an exit for:

Public Function RemoveObject(Collection As Collection, Object 
As Object)
Dim vTemp As Object
Dim Index As Integer
    Index = 1
    For Each vTemp In Collection
        If vTemp Is Object Then
            Collection.Remove Index
            Exit For
        Else
            Index = Index + 1
        End If
    Next
End Function
Andreas Hillqvist
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date