|
-
Oct 4th, 2006, 09:06 PM
#1
Thread Starter
Lively Member
[RESOLVED] Collection help[VB 2003.net]
Hi guys/gals,
Hope you guys can help me on this
I'm trying to remove items from a collection if it has a duplicate from another collection
Do anyone knows how to do this?
I can use arraylist to replace it too
I have a code here which gives me error and i'm stuck on this
Collection A = getSno
Collection B = getnew
I want to check if getSno.item = getnew.items
remove
here's my current code
VB Code:
For b = getNew.Count - 1 To 0 Step -1
If getSno.Item(b) = getNew.Item(b) Then
getSno.Remove(getNew.Item(b))
End If
msg = Boxno & "," & getSno.Item(b)
olddate.Add(msg)
pretest codes")
Next
dg1.DataSource = olddate
dg1.DataBind()
Thanks for your help guys/gals
Last edited by heeroyu16; Oct 4th, 2006 at 09:17 PM.
---------------------------------------------------
noob coder
---------------------------------------------------
-
Oct 4th, 2006, 09:43 PM
#2
Banned
Re: Collection help[VB 2003.net]
Here you go:
VB Code:
Dim a As ArrayList
Dim b As ArrayList
a = New ArrayList()
'add some data
a.Add("test")
a.Add("test2")
b = New ArrayList
'add more data
b.Add("foo")
b.Add("test2")
'loop using a
'compare one item in a
'to EVERY item in b
'if a[i]=b[j] remove b[j]
For i As Integer = 0 To a.Count - 1
For j As Integer = 0 To b.Count - 1
If a(i).ToString = b(j).ToString Then
'a[i] = b[j] in this case remove item from b
b.Remove(a(i).ToString)
End If
Next
Next
'what's left in b
'you're left with the intersection of b
'that is all elements in b that are not in a
For k As Integer = 0 To b.Count - 1
MsgBox(b(k).ToString())
Next
-
Oct 4th, 2006, 09:49 PM
#3
Re: Collection help[VB 2003.net]
If you get an error then you get an error message. What is it?
-
Oct 4th, 2006, 09:52 PM
#4
Banned
Re: Collection help[VB 2003.net]
I just can't understand why people who post dont understand we are not mind readers.
"My program errors out can you help"....ok how do we help if there is no error posted in the first place. This happens way too much in this forum.
<yoda> post the error you will, resolve the solution you shall </yoda>
<yoda> post the entire procedure you will, hand me your paycheck you shall </yoda>
How about a <yoda> tag ?
-
Oct 4th, 2006, 10:14 PM
#5
Thread Starter
Lively Member
Re: Collection help[VB 2003.net]
Opps
I always thought you guys are jedi :P
Well JakSupport your codes do help out..
Oh the the error codes are Index out of range
I know my codes will have this error cause i'm not coding correctly which is why I posted to get help no?
I've just started learning scripting and not very good in it.And it is hard for me to describe what I don't know.
Anyway thanks alot
Last edited by heeroyu16; Oct 4th, 2006 at 10:20 PM.
---------------------------------------------------
noob coder
---------------------------------------------------
-
Oct 4th, 2006, 10:30 PM
#6
Re: Collection help[VB 2003.net]
This bit removes the item at index 'b' (although in a rather peculiar way):
VB Code:
If getSno.Item(b) = getNew.Item(b) Then
getSno.Remove(getNew.Item(b))
End If
then this bit tries to access the item at index 'b':
VB Code:
msg = Boxno & "," & getSno.Item(b)
which is the very item you just removed. Having said that, If you want to remove all items from a collection that also exist in another collection then you shouldn't be comparing items at the same index. This will work if there are no dulpicate entries in the first collection:
VB Code:
For Each item As String In clctn2
If clctn1.Contains(item) Then
clctn1.Remove(item)
End If
Next item
This will work regardless:
VB Code:
For i As Integer = clctn1.Count - 1 To 0 Step -1
If clctn2.Contains(clctn1(i)) Then
clctn1.RemoveAt(i)
End If
Next i
Both methods assume that the collections contain strings. If they don't then they would need some modification.
-
Oct 4th, 2006, 10:46 PM
#7
Thread Starter
Lively Member
Re: Collection help[VB 2003.net]
Wow, u guys rock.
Both method work equally well.
Thanks again for your help.
---------------------------------------------------
noob coder
---------------------------------------------------
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
|