Contains method doesn't work on BindingList<t></t> [modified]
-
Hi i have a BindingList and i want to check if the list contains an object of ListItem. But the Contains method of the BindingList<> always returns false. can anyone tell me why? and what should i do?
BindingList<listItem> normalExtItems = new BindingList<listItem>();
//elements are added to the bindingList here
extItem = new listItem(Number,Name,Id));
if(normalExtItems.Contains(extItem))//always returns false
{
//do sth
}modified on Sunday, November 30, 2008 5:54 PM
-
Hi i have a BindingList and i want to check if the list contains an object of ListItem. But the Contains method of the BindingList<> always returns false. can anyone tell me why? and what should i do?
BindingList<listItem> normalExtItems = new BindingList<listItem>();
//elements are added to the bindingList here
extItem = new listItem(Number,Name,Id));
if(normalExtItems.Contains(extItem))//always returns false
{
//do sth
}modified on Sunday, November 30, 2008 5:54 PM
Hi, I don't see any line of code adding something to normalExtItems, so it seems obvious Contains() returns false no matter what. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Hi i have a BindingList and i want to check if the list contains an object of ListItem. But the Contains method of the BindingList<> always returns false. can anyone tell me why? and what should i do?
BindingList<listItem> normalExtItems = new BindingList<listItem>();
//elements are added to the bindingList here
extItem = new listItem(Number,Name,Id));
if(normalExtItems.Contains(extItem))//always returns false
{
//do sth
}modified on Sunday, November 30, 2008 5:54 PM
-
Since this is obviously partial code, do you insert extItem into the bindinglist somewhere. The code you posted cannot find extItem in the list because it's not added to it.
The need to optimize rises from a bad design.My articles[^]
-
yes, sure bindinglist has ListItems. Because it was in a long loop, i didn't add it to the code here. sorry it was not clear
Just a simple test case. Didn't know what your listitem type was, but it doesn't matter. The following works as expected:
System.Web.UI.WebControls.ListItem extItem;
System.ComponentModel.BindingList<System.Web.UI.WebControls.ListItem> normalExtItems
= new System.ComponentModel.BindingList<System.Web.UI.WebControls.ListItem>();
extItem = new System.Web.UI.WebControls.ListItem("A","1", true);
normalExtItems.Add(extItem);
if(normalExtItems.Contains(extItem)) {
//do sth
}Have you checked (using debugger) that the list actually contains the item you're interested in.
The need to optimize rises from a bad design.My articles[^]
-
Just a simple test case. Didn't know what your listitem type was, but it doesn't matter. The following works as expected:
System.Web.UI.WebControls.ListItem extItem;
System.ComponentModel.BindingList<System.Web.UI.WebControls.ListItem> normalExtItems
= new System.ComponentModel.BindingList<System.Web.UI.WebControls.ListItem>();
extItem = new System.Web.UI.WebControls.ListItem("A","1", true);
normalExtItems.Add(extItem);
if(normalExtItems.Contains(extItem)) {
//do sth
}Have you checked (using debugger) that the list actually contains the item you're interested in.
The need to optimize rises from a bad design.My articles[^]
-
yes, no matter the list has the item or not, it always returns false, i have checked that so many times :(
Did the test case work for you? Also the
Contains
method checks if the actual object is present in the list so if you create a similar object (all the properties are the same) it won't find it since it's not the same object.The need to optimize rises from a bad design.My articles[^]