|
-
May 13th, 2013, 07:23 PM
#1
Thread Starter
Addicted Member
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.
-
May 13th, 2013, 07:33 PM
#2
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.
-
May 13th, 2013, 07:35 PM
#3
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!
-
May 13th, 2013, 07:36 PM
#4
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!
-
May 13th, 2013, 07:41 PM
#5
Re: Removing a Specific Item from Array
 Originally Posted by dunfiddlin
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.
-
May 13th, 2013, 07:44 PM
#6
Thread Starter
Addicted Member
Re: Removing a Specific Item from Array
 Originally Posted by Niya
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!
-
May 13th, 2013, 07:50 PM
#7
Re: Removing a Specific Item from Array
 Originally Posted by digitaldrew
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.
-
May 13th, 2013, 07:54 PM
#8
Thread Starter
Addicted Member
Re: Removing a Specific Item from Array
 Originally Posted by Niya
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.
-
May 14th, 2013, 01:55 AM
#9
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.
<<<------------
.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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|