Collection Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett Wesoloski

    #1

    Collection Question

    I have a collection that I add my objects to. When I go to remove an object
    from the collect I get an error {"Cannot remove the specified item because
    it was not found in the specified Collection."}
    Now I do understand what it is saying but I know the object is in the
    collection.

    Here is some code.


    >? CurrentEPCInfo
    {KCC.MPAM.RFID. EPCCollection.E PCInfo}

    _EPC: "30540232801ADB 0000000006"

    _ID: "Kleenex"

    _Pic: "C:\\Visual Studio
    Projects\\RFID\ \KCC.MPAM.RFID. RocketCart\\KCC .MPAM.RFID.Rock etCart\\Pics\\K leenexTissue.jp g"

    EPC: "30540232801ADB 0000000006"

    ID: "Kleenex"

    PIC: "C:\\Visual Studio
    Projects\\RFID\ \KCC.MPAM.RFID. RocketCart\\KCC .MPAM.RFID.Rock etCart\\Pics\\K leenexTissue.jp g"



    ? CartEPCs[0]

    {KCC.MPAM.RFID. EPCCollection.E PCInfo}

    _EPC: "30540232801ADB 0000000006"

    _ID: "Kleenex"

    _Pic: "C:\\Visual Studio
    Projects\\RFID\ \KCC.MPAM.RFID. RocketCart\\KCC .MPAM.RFID.Rock etCart\\Pics\\K leenexTissue.jp g"

    EPC: "30540232801ADB 0000000006"

    ID: "Kleenex"

    PIC: "C:\\Visual Studio
    Projects\\RFID\ \KCC.MPAM.RFID. RocketCart\\KCC .MPAM.RFID.Rock etCart\\Pics\\K leenexTissue.jp g"



    Now they are the same.


    Here is the code I am using.


    CurrentEPCInfo. ID = "Kleenex";

    CurrentEPCInfo. EPC = "30540232801ADB 0000000006";

    CurrentEPCInfo. PIC = @"C:\Visual Studio
    Projects\RFID\K CC.MPAM.RFID.Ro cketCart\KCC.MP AM.RFID.RocketC art\Pics\Kleene xTissue.jpg";


    CartEPCs.Remove (CurrentEPCInfo );

    Now what really confuses me is if I do this

    LoadedEPCs.Add( CurrentEPCInfo) ;

    LoadedEPCs.Remo ve(CurrentEPCIn fo);


    Add it and then remove it right away it works.



    Any idea's?



    TIA,
    Brett


  • Tim Patrick

    #2
    Re: Collection Question

    When using reference types (and probably when using complex value types),
    the Remove() method only removes the specific instance of an object. It doesn't
    remove an instance that just happens to contain the same member values as
    one already in the collection. To remove the specific item, you would first
    need to get a reference to the actual instance in the collection, and then
    call Remove(), passing it that reference. For instance, the following code
    would scan through the list and remove the item you seek.
    For Each scanItem As EPCInfoClass In LoadedEPCs
    If (scanItem.EPC = "30540232801ADB 0000000006") Then
    LoadedEPCs.Remo ve(scanItem)
    Exit For
    End If
    Next scanItem
    There are other ways to accomplish this. You could override the Equals method
    for the class and use it to locate matching items.
    Public Overrides Function Equals(ByVal obj As Object) As Boolean
    ' ----- Allow IndexOf() and Contains() searches.
    If (TypeOf obj Is String) Then
    Return CBool(CStr(obj) = Me.EPC)
    Else
    Return MyBase.Equals(o bj)
    End If
    End Function
    Then you could use a statement like this.
    LoadedEPCs.Remo veAt(LoadedEPCs .IndexOf("30540 232801ADB000000 0006"))
    -----
    Tim Patrick
    Start-to-Finish Visual Basic 2005
    I have a collection that I add my objects to. When I go to remove an
    object
    from the collect I get an error {"Cannot remove the specified item
    because
    it was not found in the specified Collection."}
    Now I do understand what it is saying but I know the object is in the
    collection.
    Here is the code I am using.
    >
    CurrentEPCInfo. ID = "Kleenex";
    >
    CurrentEPCInfo. EPC = "30540232801ADB 0000000006";
    >
    CurrentEPCInfo. PIC = @"C:\Visual Studio
    Projects\RFID\K CC.MPAM.RFID.Ro cketCart\KCC.MP AM.RFID.RocketC art\Pics\K
    leenexTissue.jp g";
    >
    CartEPCs.Remove (CurrentEPCInfo );
    >
    Now what really confuses me is if I do this
    >
    LoadedEPCs.Add( CurrentEPCInfo) ;
    >
    LoadedEPCs.Remo ve(CurrentEPCIn fo);
    >
    Add it and then remove it right away it works.
    >
    Any idea's?
    >
    TIA,
    Brett

    Comment

    • Brett Wesoloski

      #3
      Re: Collection Question

      Ah, thanks! That makes sense.


      "Tim Patrick" <invalid@invali d.com.invalidwr ote in message
      news:e3b469760c d98c8c564446a6b c2@newsgroups.c omcast.net...
      When using reference types (and probably when using complex value types),
      the Remove() method only removes the specific instance of an object. It
      doesn't remove an instance that just happens to contain the same member
      values as one already in the collection. To remove the specific item, you
      would first need to get a reference to the actual instance in the
      collection, and then call Remove(), passing it that reference. For
      instance, the following code would scan through the list and remove the
      item you seek.
      >
      >For Each scanItem As EPCInfoClass In LoadedEPCs
      > If (scanItem.EPC = "30540232801ADB 0000000006") Then
      > LoadedEPCs.Remo ve(scanItem)
      > Exit For
      > End If
      >Next scanItem
      >
      There are other ways to accomplish this. You could override the Equals
      method for the class and use it to locate matching items.
      >
      > Public Overrides Function Equals(ByVal obj As Object) As Boolean
      > ' ----- Allow IndexOf() and Contains() searches.
      > If (TypeOf obj Is String) Then
      > Return CBool(CStr(obj) = Me.EPC)
      > Else
      > Return MyBase.Equals(o bj)
      > End If
      > End Function
      >
      Then you could use a statement like this.
      >
      >LoadedEPCs.Rem oveAt(LoadedEPC s.IndexOf("3054 0232801ADB00000 00006"))
      >
      -----
      Tim Patrick
      Start-to-Finish Visual Basic 2005
      >
      >I have a collection that I add my objects to. When I go to remove an
      >object
      >from the collect I get an error {"Cannot remove the specified item
      >because
      >it was not found in the specified Collection."}
      >Now I do understand what it is saying but I know the object is in the
      >collection.
      >Here is the code I am using.
      >>
      >CurrentEPCInfo .ID = "Kleenex";
      >>
      >CurrentEPCInfo .EPC = "30540232801ADB 0000000006";
      >>
      >CurrentEPCInfo .PIC = @"C:\Visual Studio
      >Projects\RFID\ KCC.MPAM.RFID.R ocketCart\KCC.M PAM.RFID.Rocket Cart\Pics\K
      >leenexTissue.j pg";
      >>
      >CartEPCs.Remov e(CurrentEPCInf o);
      >>
      >Now what really confuses me is if I do this
      >>
      >LoadedEPCs.Add (CurrentEPCInfo );
      >>
      >LoadedEPCs.Rem ove(CurrentEPCI nfo);
      >>
      >Add it and then remove it right away it works.
      >>
      >Any idea's?
      >>
      >TIA,
      >Brett
      >
      >

      Comment

      Working...