|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Collection Questionfrom 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.EPCInfo}_EPC: "30540232801ADB0000000006" _ID: "Kleenex" _Pic: "C:\\Visual Studio Projects\\RFID\\KCC.MPAM.RFID.RocketCart\\KCC.MPAM.RFID.RocketCart\\Pics\\KleenexTissue.jpg" EPC: "30540232801ADB0000000006" ID: "Kleenex" PIC: "C:\\Visual Studio Projects\\RFID\\KCC.MPAM.RFID.RocketCart\\KCC.MPAM.RFID.RocketCart\\Pics\\KleenexTissue.jpg" ? CartEPCs[0] {KCC.MPAM.RFID.EPCCollection.EPCInfo} _EPC: "30540232801ADB0000000006" _ID: "Kleenex" _Pic: "C:\\Visual Studio Projects\\RFID\\KCC.MPAM.RFID.RocketCart\\KCC.MPAM.RFID.RocketCart\\Pics\\KleenexTissue.jpg" EPC: "30540232801ADB0000000006" ID: "Kleenex" PIC: "C:\\Visual Studio Projects\\RFID\\KCC.MPAM.RFID.RocketCart\\KCC.MPAM.RFID.RocketCart\\Pics\\KleenexTissue.jpg" Now they are the same. Here is the code I am using. CurrentEPCInfo.ID = "Kleenex"; CurrentEPCInfo.EPC = "30540232801ADB0000000006"; CurrentEPCInfo.PIC = @"C:\Visual Studio Projects\RFID\KCC.MPAM.RFID.RocketCart\KCC.MPAM.RFID.RocketCart\Pics\KleenexTissue.jpg"; CartEPCs.Remove(CurrentEPCInfo); Now what really confuses me is if I do this LoadedEPCs.Add(CurrentEPCInfo); LoadedEPCs.Remove(CurrentEPCInfo); Add it and then remove it right away it works. Any idea's? TIA, Brett 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 There are other ways to accomplish this. You could override the Equals method > If (scanItem.EPC = "30540232801ADB0000000006") Then > LoadedEPCs.Remove(scanItem) > Exit For > End If > Next scanItem for the class and use it to locate matching items. > Public Overrides Function Equals(ByVal obj As Object) As Boolean Then you could use a statement like this.> ' ----- Allow IndexOf() and Contains() searches. > If (TypeOf obj Is String) Then > Return CBool(CStr(obj) = Me.EPC) > Else > Return MyBase.Equals(obj) > End If > End Function > LoadedEPCs.RemoveAt(LoadedEPCs.IndexOf("30540232801ADB0000000006")) -----Tim Patrick Start-to-Finish Visual Basic 2005 Show quoteHide quote > 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 = "30540232801ADB0000000006"; > > CurrentEPCInfo.PIC = @"C:\Visual Studio > Projects\RFID\KCC.MPAM.RFID.RocketCart\KCC.MPAM.RFID.RocketCart\Pics\K > leenexTissue.jpg"; > > CartEPCs.Remove(CurrentEPCInfo); > > Now what really confuses me is if I do this > > LoadedEPCs.Add(CurrentEPCInfo); > > LoadedEPCs.Remove(CurrentEPCInfo); > > Add it and then remove it right away it works. > > Any idea's? > > TIA, > Brett Ah, thanks! That makes sense.
Show quoteHide quote "Tim Patrick" <inva***@invalid.com.invalid> wrote in message news:e3b469760cd98c8c564446a6bc2@newsgroups.comcast.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 = "30540232801ADB0000000006") Then >> LoadedEPCs.Remove(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(obj) >> End If >> End Function > > Then you could use a statement like this. > >> LoadedEPCs.RemoveAt(LoadedEPCs.IndexOf("30540232801ADB0000000006")) > > ----- > 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 = "30540232801ADB0000000006"; >> >> CurrentEPCInfo.PIC = @"C:\Visual Studio >> Projects\RFID\KCC.MPAM.RFID.RocketCart\KCC.MPAM.RFID.RocketCart\Pics\K >> leenexTissue.jpg"; >> >> CartEPCs.Remove(CurrentEPCInfo); >> >> Now what really confuses me is if I do this >> >> LoadedEPCs.Add(CurrentEPCInfo); >> >> LoadedEPCs.Remove(CurrentEPCInfo); >> >> Add it and then remove it right away it works. >> >> Any idea's? >> >> TIA, >> Brett > >
Systray Icon
Regular Expressions .NET Issue with VB6 using a .NET DLL on one PC with Windows 2000 Server how to round number to custom step (0.25, 20, 100...) Determine what current drive letter is? Bug in Radio Button (or weird feature) audio functions in Winmm.dll Compilation in dot NET Quick SQLclient Data Access ASPX / VB .NET Combine a SQL connection with a QDBC connection |
|||||||||||||||||||||||