Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. is it possible to find List item belongings?

is it possible to find List item belongings?

Scheduled Pinned Locked Moved C#
dockerhelpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    c0ax_lx
    wrote on last edited by
    #1

    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!

    L 1 Reply Last reply
    0
    • C c0ax_lx

      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!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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


      C 1 Reply Last reply
      0
      • L Luc Pattyn

        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


        C Offline
        C Offline
        c0ax_lx
        wrote on last edited by
        #3

        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!

        P 1 Reply Last reply
        0
        • C c0ax_lx

          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!

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          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.

          C 1 Reply Last reply
          0
          • P Pete OHanlon

            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.

            C Offline
            C Offline
            c0ax_lx
            wrote on last edited by
            #5

            probably! i just need to gather some courage. i'm scared of professional software architects ;}

            If it' stuck, DO NOT pull harder!

            P 1 Reply Last reply
            0
            • C c0ax_lx

              probably! i just need to gather some courage. i'm scared of professional software architects ;}

              If it' stuck, DO NOT pull harder!

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups