By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,959 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,959 IT Pros & Developers. It's quick & easy.

Collection item property

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Jul 21 '05 #2

P: n/a
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.
"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:uY**************@tk2msftngp13.phx.gbl...
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


Jul 21 '05 #3

P: n/a
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

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:uY**************@tk2msftngp13.phx.gbl...
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


Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.