is it possible to find List item belongings?
-
if i have a List<> of controls. i can add those controls to a container and use the .Parent property. but is there any way to find out which List<> ('s) the control belongs to? i know i can do it the other way with .Contains on the list but then i would need a list of lists to enumerate. Am i missing something obvious here?
If it' stuck, DO NOT pull harder!
-
if i have a List<> of controls. i can add those controls to a container and use the .Parent property. but is there any way to find out which List<> ('s) the control belongs to? i know i can do it the other way with .Contains on the list but then i would need a list of lists to enumerate. Am i missing something obvious here?
If it' stuck, DO NOT pull harder!
Hi, this is how I see it: - a Control can be shown in some Container, and Parent will reflect that; there is only one Parent, since the Control is in one Container at most. - an object (Control or other) can be part of many collections at the same time; so there is no property that points from that object to the collection(s) it is in. - if you somehow make sure an object is in exactly one of several collections, you can reflect that fact in several ways, here are two of them: (a) create a Hashtable or Dictionary, use the object/Control as key, and the collection it belongs to as value; doing so, a single Hashtable/Dictionary lookup will return the collection (b) a Control has a Tag property that is not used by Windows or the .NET Framework; it has type Object and can be used for whatever purpose you see fit. So you could store a reference to the collection in that Tag. Three more comments: 1. in both cases you would have to provide code to keep things correct (insert/replace the Hashtable/Dictionary entry when the Control is inserted into or mode among collections 2. when using the Tag property or the Hashtable you would need to cast the value to a Control (or a more specialized type such as Label) when you retrieve it. 3. The proposed ways avoid the explicit search at the expence of extra data; this data redundancy could result in an inconsistent state. If you don't like that, the solution is to search the collections, as you were mentioning. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
Hi, this is how I see it: - a Control can be shown in some Container, and Parent will reflect that; there is only one Parent, since the Control is in one Container at most. - an object (Control or other) can be part of many collections at the same time; so there is no property that points from that object to the collection(s) it is in. - if you somehow make sure an object is in exactly one of several collections, you can reflect that fact in several ways, here are two of them: (a) create a Hashtable or Dictionary, use the object/Control as key, and the collection it belongs to as value; doing so, a single Hashtable/Dictionary lookup will return the collection (b) a Control has a Tag property that is not used by Windows or the .NET Framework; it has type Object and can be used for whatever purpose you see fit. So you could store a reference to the collection in that Tag. Three more comments: 1. in both cases you would have to provide code to keep things correct (insert/replace the Hashtable/Dictionary entry when the Control is inserted into or mode among collections 2. when using the Tag property or the Hashtable you would need to cast the value to a Control (or a more specialized type such as Label) when you retrieve it. 3. The proposed ways avoid the explicit search at the expence of extra data; this data redundancy could result in an inconsistent state. If you don't like that, the solution is to search the collections, as you were mentioning. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
Thanks for the reply. this is just one of those "i wonder what the best way to solve X is" thaughts that just wont leave my brain ;} i had a project a couple of weeks ago where i needed to have a reference to the "parent list" which i solved by overloading the controls and adding a ParentList property. but that never felt like "best practice". i needed the .Tag for other purposes CP should have a Philosophy message type ;}
If it' stuck, DO NOT pull harder!
-
Thanks for the reply. this is just one of those "i wonder what the best way to solve X is" thaughts that just wont leave my brain ;} i had a project a couple of weeks ago where i needed to have a reference to the "parent list" which i solved by overloading the controls and adding a ParentList property. but that never felt like "best practice". i needed the .Tag for other purposes CP should have a Philosophy message type ;}
If it' stuck, DO NOT pull harder!
c0ax_lx wrote:
CP should have a Philosophy message type ;}
Actually, this would fit quite nicely into the Design and Architecture section. It would certainly trigger a debate.
Deja View - the feeling that you've seen this post before.
-
c0ax_lx wrote:
CP should have a Philosophy message type ;}
Actually, this would fit quite nicely into the Design and Architecture section. It would certainly trigger a debate.
Deja View - the feeling that you've seen this post before.
-
probably! i just need to gather some courage. i'm scared of professional software architects ;}
If it' stuck, DO NOT pull harder!
Don't be. Most of the people who answer on that forum are professional developers rather than architects.
Deja View - the feeling that you've seen this post before.