locked
Where do I put a collection of my own classes? RRS feed

  • Question

  • Hello.

    So far, my application consists of a canvas in a window.

    I also made a class called "ShapeThing" and it has a List<Point> called "PointList" as a property. Inside of PointList I store a list of points which are created and added to the list when the user clicks on the canvas.

    However, I need a collection where I can store the ShapeThing class so I can get to the list of points when I need them.

    My question is, where do I put the collection of ShapeThings?

    Also, what kind of collection should I use? List<ShapeThing>?
    Thursday, January 8, 2009 11:25 PM

Answers

  • That would work over the short haul.  But, you might be better off over the long run defining your own generic collection or class.  Did you know that you can inherit from List<>? 

    It might also be a good time to consider defining an interface or two.  Use those to reference your objects through a contract instead of directly to a an instance.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by animas3D Friday, January 9, 2009 4:07 AM
    Thursday, January 8, 2009 11:43 PM
    Moderator
  • Actually, If inheritence is what you have in mind, you should implement from ICollections<T> rather then List<T>.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by animas3D Friday, January 9, 2009 4:07 AM
    • Unmarked as answer by animas3D Friday, January 9, 2009 4:07 AM
    • Marked as answer by animas3D Friday, January 9, 2009 4:07 AM
    Friday, January 9, 2009 12:17 AM

All replies

  • That would work over the short haul.  But, you might be better off over the long run defining your own generic collection or class.  Did you know that you can inherit from List<>? 

    It might also be a good time to consider defining an interface or two.  Use those to reference your objects through a contract instead of directly to a an instance.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by animas3D Friday, January 9, 2009 4:07 AM
    Thursday, January 8, 2009 11:43 PM
    Moderator
  • Actually, If inheritence is what you have in mind, you should implement from ICollections<T> rather then List<T>.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by animas3D Friday, January 9, 2009 4:07 AM
    • Unmarked as answer by animas3D Friday, January 9, 2009 4:07 AM
    • Marked as answer by animas3D Friday, January 9, 2009 4:07 AM
    Friday, January 9, 2009 12:17 AM
  • Thanks for your help. I will try to define my own generic collection.

    What is the difference between ICollections<T> and Collections<T>?
    Friday, January 9, 2009 4:20 AM
  • ICollections<T> is an interface that defines the methods and properties that you need to implement.

    Collections<T> is a generic class that allows you to inherit al buttload of premade code to work.  Depending on what you need to do, either one will work.

    If you only need a default storage for your classes with little more, then I would just do something like this:

    public class ShapeThingCollection : Collection<ShapeThing>  
    {  
        //that's it  

    Now you can create a ShapeThingCollection and have it typed to your ShapeThing class with little more to do.

    Now if you aren't liking what the Collection<T> does, you can implement the Interface, but this is what you would have to code for it to actually work:

    public class ShapeThingCollection : System.Collections.Generic.ICollection<ShapeThing>  
        {
            #region ICollection<ShapeThing> Members  
     
            public void Add(ShapeThing item)  
            {  
                throw new NotImplementedException();  
            }  
     
            public void Clear()  
            {  
                throw new NotImplementedException();  
            }  
     
            public bool Contains(ShapeThing item)  
            {  
                throw new NotImplementedException();  
            }  
     
            public void CopyTo(ShapeThing[] array, int arrayIndex)  
            {  
                throw new NotImplementedException();  
            }  
     
            public int Count  
            {  
                get 
                {  
                    throw new NotImplementedException();  
                }  
            }  
     
            public bool IsReadOnly  
            {  
                get 
                {  
                    throw new NotImplementedException();  
                }  
            }  
     
            public bool Remove(ShapeThing item)  
            {  
                throw new NotImplementedException();  
            }
            #endregion  
     
            #region IEnumerable<ShapeThing> Members  
     
            public System.Collections.Generic.IEnumerator<ShapeThing> GetEnumerator()  
            {  
                throw new NotImplementedException();  
            }
            #endregion  
     
            #region IEnumerable Members  
     
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()  
            {  
                throw new NotImplementedException();  
            }
            #endregion  
        } 

    This will allow you to define exactly what you want though.
    There are 10 types of people in this world, those who understand Binary, and those who don't.
    Friday, January 9, 2009 1:55 PM
  • Wow. I sort of dig what you're saying, but the latter part is kind of over my head right now. I'll stick to the easy one for now.

    I will refer back to the other stuff as I gain more knowledge, however. Thanks a lot.
    Saturday, January 10, 2009 2:20 AM