Results 1 to 7 of 7

Thread: [RESOLVED] Collection help[VB 2003.net]

  1. #1

    Thread Starter
    Lively Member heeroyu16's Avatar
    Join Date
    Nov 2005
    Posts
    123

    Resolved [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:
    1. For b = getNew.Count - 1 To 0 Step -1
    2.                         If getSno.Item(b) = getNew.Item(b) Then
    3.                             getSno.Remove(getNew.Item(b))
    4.                         End If
    5.  
    6.                         msg = Boxno & "," & getSno.Item(b)
    7.                         olddate.Add(msg)
    8.                         pretest codes")
    9.               Next
    10.                     dg1.DataSource = olddate
    11.                     dg1.DataBind()

    Thanks for your help guys/gals
    Last edited by heeroyu16; Oct 4th, 2006 at 09:17 PM.
    ---------------------------------------------------
    noob coder
    ---------------------------------------------------

  2. #2
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: Collection help[VB 2003.net]

    Here you go:

    VB Code:
    1. Dim a As ArrayList
    2.         Dim b As ArrayList
    3.  
    4.         a = New ArrayList()
    5.         'add some data
    6.         a.Add("test")
    7.         a.Add("test2")
    8.  
    9.         b = New ArrayList
    10.         'add more data
    11.         b.Add("foo")
    12.         b.Add("test2")
    13.  
    14.         'loop using a
    15.         'compare one item in a
    16.         'to EVERY item in b
    17.         'if a[i]=b[j] remove b[j]
    18.         For i As Integer = 0 To a.Count - 1
    19.             For j As Integer = 0 To b.Count - 1
    20.                 If a(i).ToString = b(j).ToString Then
    21.                     'a[i] = b[j] in this case remove item from b
    22.                     b.Remove(a(i).ToString)
    23.                 End If
    24.             Next
    25.         Next
    26.  
    27.         'what's left in b
    28.         'you're left with the intersection of b
    29.         'that is all elements in b that are not in a
    30.         For k As Integer = 0 To b.Count - 1
    31.             MsgBox(b(k).ToString())
    32.         Next

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Collection help[VB 2003.net]

    If you get an error then you get an error message. What is it?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Banned
    Join Date
    May 2006
    Posts
    161

    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 ?

  5. #5

    Thread Starter
    Lively Member heeroyu16's Avatar
    Join Date
    Nov 2005
    Posts
    123

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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Collection help[VB 2003.net]

    This bit removes the item at index 'b' (although in a rather peculiar way):
    VB Code:
    1. If getSno.Item(b) = getNew.Item(b) Then
    2.     getSno.Remove(getNew.Item(b))
    3. End If
    then this bit tries to access the item at index 'b':
    VB Code:
    1. 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:
    1. For Each item As String In clctn2
    2.     If clctn1.Contains(item) Then
    3.         clctn1.Remove(item)
    4.     End If
    5. Next item
    This will work regardless:
    VB Code:
    1. For i As Integer = clctn1.Count - 1 To 0 Step -1
    2.     If clctn2.Contains(clctn1(i)) Then
    3.         clctn1.RemoveAt(i)
    4.     End If
    5. Next i
    Both methods assume that the collections contain strings. If they don't then they would need some modification.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member heeroyu16's Avatar
    Join Date
    Nov 2005
    Posts
    123

    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
  •  



Click Here to Expand Forum to Full Width