473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Collection item property

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
3 2508
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
3
by: jason | last post by:
Hello. I've got this simple collection populate code I downloaded from the net (sorry can't find source now) I'm trying to test, but I can't seem to get it to work. Any help would be greatly...
2
by: S. Justin Gengo | last post by:
Hi, I've created a component that allows me to store database information for various types of databases my company uses. It uses a collection for each type of database. Everything is working...
4
by: Michael K. Walter | last post by:
I'd like to create a strongly-typed collection of objects that are indexed by a string value (key is a string). I'd also like to allow users to iterate over the collection using a For-each loop...
3
by: Thief_ | last post by:
I want to store data pertaining to a widget in a collection. The widget has many pproperties, so i created the following class and called it "clsWatchProperties": Public Class clsWatchProperties...
4
by: Michael | last post by:
Dear all .. If I want to use develop a user control and declare a public property which the type is System.Windows.Forms.GridTableStylesCollection For example : Public Class LookAndView...
3
by: Marc L'Ecuyer | last post by:
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...
14
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.