Collection item property

M

Marc L'Ecuyer

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
 
H

Herfried K. Wagner [MVP]

Marc L'Ecuyer said:
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?

You will have to loop through all items if a new item is added and
update their 'Selected' property. Are you sure the question is related
to Windows Forms?
 
P

Peter Rilling

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;
}
 
P

Peter Rilling

You can adapt this method to your collections that you can set the
supporting class when the item is added to the collection rather than when
you create the item. This also allows you to have more than one collection
that are independent where you can have an item selected in each of your
classes without affecting each other.
 
J

Jay B. Harlow [MVP - Outlook]

Peter,
I would make the collection itself the support class.

In that the collection itself would have the Selected property.

Which somewhat combines this and your next post into the Collection class,
rather than having the Collection class & the class to track Selected.
(Sometimes you do need two classes ;-))

If a MyItem could only be owned by one collection (ala Windows Forms
Controls), you could have an event in MyItem that notified its collection
that it was selected, updating the selection property in the collection.
MyItem could check its owner to see if the current instance is the selected
item.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top