Here is an idea. Rather than having each MyItem instance have a boolean
that indicates if it is selected or not, it might be better to have a
support class that all instances of the class shares. That support class
might have a single property that contains the instance of the item that is
selected. When an item is selected, it simply places itself in this
supporting class. When you ask an item if it is selected, you would simply
check for reference equality to see if the item that is being checked is
that same item that is in the support class.
That way your evaluation is always O(1).
Below is a rough, uncompiled, sample that might get you started.
public class MyItem{
public MyItem(ItemSelection selInstance){
__sel = selInstance;
}
public bool Selected{
get{return Object.ReferenceEquals(this, __sel.CurrentSelection)}
set{__sel.CurrentSelection = this;}
}
}
public class ItemSelection{
public CurrentSelection{
get{return __selection;}
set{__selection = value;}
}
private MyItem __selection;
}
"Marc L'Ecuyer" <ml******@chca.ca> wrote in message
news:uK*************@TK2MSFTNGP11.phx.gbl...
Hi,
I have a collection class derived from CollectionBase. This collection can
add items of my class MyItem. In my class MyItem, I have a Selected
property. When this property is set to true, I have to set this property
to false for all other items in the collection (only 1 item can be selected).
How can I do that?
Thanks
Marc