Results 1 to 9 of 9

Thread: Removing a Specific Item from Array

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    158

    Removing a Specific Item from Array

    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.

    Code:
        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

    Code:
        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

    Code:
        Public Sub RemoveElement()
            Dim I As Integer
            For I = cnt1 To UBound(TempArray1) - 1
                TempArray1(I) = TempArray1(I + 1)
            Next I
        End Sub
    Last edited by digitaldrew; May 13th, 2013 at 07:28 PM.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Removing a Specific Item from Array

    I have a suggestion that would make all this 100x simpler. Use a List(Of T) instead of arrays. The List(Of T) has methods to remove elements from it. That code you posted is the kind of thing you'd do in VB6. You don't have to torture yourself like that in VB.Net.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Removing a Specific Item from Array

    You can't remove an element from the midst of an array because it's a fixed length object. If you redim it to fewer items than it will simply truncate. I admit I didn't attempt to follow the logic of your 'solution' but then I knew it wouldn't work already!

    If you want to literally remove items then choose an object which has a Remove method; List, ListBox.Items etc.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Removing a Specific Item from Array

    That code you posted is the kind of thing you'd do in VB6.
    It wouldn't have worked any better though. They ain't changed the specifications of arrays since ... well ... ever!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Removing a Specific Item from Array

    Quote Originally Posted by dunfiddlin View Post
    It wouldn't have worked any better though. They ain't changed the specifications of arrays since ... well ... ever!
    Thing is though, in VB6 you had no choice. You could have opted to use the Collection class but that thing is so broken by trying to be a dictionary and a collection at the same time that its best to avoid it to preserve your sanity.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    158

    Re: Removing a Specific Item from Array

    Quote Originally Posted by Niya View Post
    I have a suggestion that would make all this 100x simpler. Use a List(Of T) instead of arrays. The List(Of T) has methods to remove elements from it. That code you posted is the kind of thing you'd do in VB6. You don't have to torture yourself like that in VB.Net.
    The registrar requires domains be sent in an array..not a List.

    Thanks for the input everyone!

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Removing a Specific Item from Array

    Quote Originally Posted by digitaldrew View Post
    The registrar requires domains be sent in an array..not a List.
    Doesn't much a difference. An array can be converted to a List(Of T) by calling its ToList method. You can convert the arrays to lists, remove, add etc to it and you can convert the list or lists back into arrays by calling ToArray.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    158

    Re: Removing a Specific Item from Array

    Quote Originally Posted by Niya View Post
    Doesn't much a difference. An array can be converted to a List(Of T) by calling its ToList method. You can convert the arrays to lists, remove, add etc to it and you can convert the list or lists back into arrays by calling ToArray.
    Thanks for the suggestion. I'll look into this.

  9. #9
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Removing a Specific Item from Array

    Manage the data in a List(of T) as mentioned, and when you are ready to send it in, cast it to an array with the ToArray() method. Removing an element from an array and resizing it accordingly, or creating a new fixed sized array would be just a bunch of fooling around.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

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