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 object part of CollectionBase

is object part of CollectionBase

Scheduled Pinned Locked Moved C#
question
6 Posts 5 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.
  • H Offline
    H Offline
    Hanzaplast
    wrote on last edited by
    #1

    hi guys can i get Collection from object if it is part of it? code should look something like this

    public class SomeCollection : CollectionBase
    {
    public void Add(SomeClass sc)
    {
    this.List.Add(sc);
    }
    }

    class SomeClass
    {
    public bool AmIPartOfCollection
    {
    get
    {
    //return false or true
    }
    }

    public SomeCollection MyCollection
    {
    get
    {
    //if this object is part of SomeCollection return his SomeCollection
    }
    }
    }

    void Main()
    {
    SomeClass temp = new SomeClass();
    //temp.AmIPartOfCollection - want to get false
    //temp.MyCollection - want to get null

    SomeCollection sc = new SomeCollection();
    sc.Add(temp);
    
    //temp.AmIPartOfCollection - want to get true
    //temp.MyCollection - want to get reference to "sc" object
    

    }

    B L 2 Replies Last reply
    0
    • H Hanzaplast

      hi guys can i get Collection from object if it is part of it? code should look something like this

      public class SomeCollection : CollectionBase
      {
      public void Add(SomeClass sc)
      {
      this.List.Add(sc);
      }
      }

      class SomeClass
      {
      public bool AmIPartOfCollection
      {
      get
      {
      //return false or true
      }
      }

      public SomeCollection MyCollection
      {
      get
      {
      //if this object is part of SomeCollection return his SomeCollection
      }
      }
      }

      void Main()
      {
      SomeClass temp = new SomeClass();
      //temp.AmIPartOfCollection - want to get false
      //temp.MyCollection - want to get null

      SomeCollection sc = new SomeCollection();
      sc.Add(temp);
      
      //temp.AmIPartOfCollection - want to get true
      //temp.MyCollection - want to get reference to "sc" object
      

      }

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      Not with a standard collection, no. It's pretty straightforward to write a specialised collection and interface for member objects to do this. However, in most situations you know roughly where the object must be, so you can use targetCollection.Contains.

      1 Reply Last reply
      0
      • H Hanzaplast

        hi guys can i get Collection from object if it is part of it? code should look something like this

        public class SomeCollection : CollectionBase
        {
        public void Add(SomeClass sc)
        {
        this.List.Add(sc);
        }
        }

        class SomeClass
        {
        public bool AmIPartOfCollection
        {
        get
        {
        //return false or true
        }
        }

        public SomeCollection MyCollection
        {
        get
        {
        //if this object is part of SomeCollection return his SomeCollection
        }
        }
        }

        void Main()
        {
        SomeClass temp = new SomeClass();
        //temp.AmIPartOfCollection - want to get false
        //temp.MyCollection - want to get null

        SomeCollection sc = new SomeCollection();
        sc.Add(temp);
        
        //temp.AmIPartOfCollection - want to get true
        //temp.MyCollection - want to get reference to "sc" object
        

        }

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

        Hanzaplast wrote:

        public SomeCollection MyCollection

        Hanzaplast wrote:

        //if this object is part of SomeCollection return his SomeCollection

        you can't do that in general as an object may be part of more than one collection. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        D 1 Reply Last reply
        0
        • L Luc Pattyn

          Hanzaplast wrote:

          public SomeCollection MyCollection

          Hanzaplast wrote:

          //if this object is part of SomeCollection return his SomeCollection

          you can't do that in general as an object may be part of more than one collection. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Damn! Beat me to it... My 5.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          L 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Damn! Beat me to it... My 5.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

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

            you could have suggested a

            public List MyCollections {get;}

            :-D

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            B 1 Reply Last reply
            0
            • L Luc Pattyn

              you could have suggested a

              public List MyCollections {get;}

              :-D

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              The inference I take from your answer, Luc (the answer seems "intuitively" correct to me), is that you could then iterate MyCollections, and, for each Collection in MyCollection, test for the presence of a specifc object using 'Contains. This solution also seeming to allow for the idea that you could also, then ... in the case of an object being a member of more than one Collection ... return a List of all Collections it was a member in, as well. However, I do think the OP's code is pretty weird. If you are going to add any type of object to a collection: that would seem to indicate a need to me for a generic (even "dynamic," late-bound ?) solution. thanks, Bill

              "It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle

              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