Question Removing a Specific Item from Array

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
Hey everyone..I could really use some help with removing a specific item from an array. Here is what I have so far..

Right now I have two arrays. The first array holds a list of domain names that will be checked to see if they are available for registration. If one or more of those domains is available then they will be put into a second array and sent over to get registered. Once the domains in the second array are successfully registered I want to remove them from the first array so they are not checked for availability again (since I've just registered them)..

My current code is suppose to copy the main array (with all domains) to a temporary array - remove the registered domain(s) from the second array and then copy that temporary array (now with removed elements) back over to the main array so it can start from the beginning. The problem is, it doesn't actually remove the correct elements..It simply removes the last domain in the array. What this means, is that if I check a list of 5 domains and the domain in position 3 gets registered - it won't remove it from the array. Instead, the domain in the last position will be removed.

VB.NET:
    Public Sub ArrayCompare()
        Dim tmpcnt As Integer
        Dim cnt1 As Integer
        Dim cnt3 As Integer
        Dim cnt4 As Integer

        cnt1 = 0
        cnt3 = 0
        cnt4 = 0
        'Find the location of the registered domains in the main array
        tmpcnt = registeredArray.Length
        While cnt3 <= mainArray.Length - 1
            'here it checks the mainArray to see if it containts the registeredArray domain
            While cnt4 <= availablecnt
                If InStr(mainArray(cnt3), registeredArray(cnt4)) = 0 Then
                    'do nothing
                ElseIf InStr(mainArray(cnt3), registeredArray(cnt4)) > 0 Then
                    TempArray1 = mainArray
                    RemoveItem2()
                    mainArray = TempArray1
                    If tmpcnt = 1 Then
                        Exit Sub
                    End If
                Else
                    txtLog.AppendText("Error With ArrayCompare: " & registeredArray(cnt4) & vbCrLf)
                End If
                cnt4 = cnt4 + 1
            End While
            cnt4 = 0
            cnt3 = cnt3 + 1
        End While
        ReDim Preserve mainArray(UBound(mainArray) - 1)

    End Sub

and

VB.NET:
    Public Sub RemoveItem2()
        Dim TempDelElement As String
        For Me.cnt2 = 0 To DeleteArray.Length - 1
            TempDelElement = DeleteArray(cnt2)
            cnt1 = 0
            While cnt1 < TempArray1.Length - 1
                If TempDelElement = TempArray1(cnt1) Then
                    RemoveElement()
                End If
                cnt1 = cnt1 + 1
            End While
        Next
    End Sub

and

VB.NET:
    Public Sub RemoveElement()
        Dim I As Integer
        For I = cnt1 To UBound(TempArray1) - 1
            TempArray1(I) = TempArray1(I + 1)
        Next I
    End Sub
 
You really shouldn't be using an array. Arrays are fixed-size so, while you can set an element to Nothing, you can't remove it. I always think of arrays as being like egg cartons. The carton doesn't change size and, while you can remove an egg, the cup remains part of the carton. You should be using a collection rather than an array. Possibly two collections in fact. E.g.
Dim allItems As New List(Of SomeType)
Dim itemsForRegistration As New List(Of SomeType)

'...

For Each item In allItems
    If Not IsRegistered(item) Then
        itemsForRegistration.Add(item)
    End If
Next

'...

For Each item In itemsForRegistration
    allItems.Remove(item)
Next

itemsForRegistration.Clear()
 
Hey jmcil..Thanks a bunch for your response! I've been reading some more and it appears a ToList method may be better for this? I'm going to try implementing your example above and see what happens. Thanks for this!
 
Back
Top