devxlogo

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

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 ObjectDim Index As Integer    Index = 1    For Each vTemp In Collection        If vTemp Is Object Then            Collection.Remove Index        Else            Index = Index + 1        End If    NextEnd FunctionIf 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 ObjectDim 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    NextEnd Function

See also  Does It Make Sense to Splurge on a Laptop?
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist