Class Design with Collection Classes

J

Julie

Hello everyone,
You will have to forgive my newbish questions...

I have a class that has a sub collection class.

Public Class Item
Public Collection As ItemCollection

Public Class ItemCollection
Inherits Generic.List(Of Item)
End Class
End Class

I have two questions:
1. When is it appropriate to use a sub class like above? I got the idea from
System.Windows.Forms.Control. But when i look at
System.Windows.Forms.TreeNode, it doesn't have its collection class as a sub
class (System.Windows.Forms.TreeNodeCollection). Is there a web site the
defines the best practices for class and subclass usage?

2. If i were to inherit Item into ItemDetail, the ItemCollection wouldn't be
type specific for ItemDetail. So what would I do in this case? Create
another sub collection class? I don't think ItemDetailCollection could
inherit ItemCollection.

I'd appreciate any help anybody can give me.

Thank you very much in advance,

Julie.
 
C

Cor Ligthert[MVP]

There are endless constructions to create with Net.
Would you not ask yourself better, "how can I solve something? instead of
how can I create a crazy construction.
I know, knitting can be very funny also for me, but when it only creates a
knot, then it is more a kind of sculpture.

It needs in the list a reference to its own class.

By the way, the TreeNodeCollection is older then the generic list so I wont
assume that it has this construction.

Jmo

Cor
 
J

Julie

Surely though, there must be some standard design principals behind the use
of sub-classes?

When it boils down, that is my actual question... When to use and when not
to use sub-classes.

Do you know of any information I can look up on this?
 
T

Tom Shelton

Surely though, there must be some standard design principals behind the use
of sub-classes?

When it boils down, that is my actual question... When to use and when not
to use sub-classes.

Do you know of any information I can look up on this?

I don't know - it's pretty subjective. There are some rules that have sort of
perculated out of the whole pattern thing... Things like:

1) Program to interfaces not implementations
2) Favor composition over inheritance
3) Isolate the parts of an application that vary, from those that don't.
4) Code should be closed to change, but open to extension...

etc, etc. I suggest you study up on design patterns. I'm no expert - but, I
feel that becoming familiar with the concept of design patterns and studying
them a bit sort of opend the door to the "next level" so to speak :)
 
A

Armin Zingler

Am 23.03.2010 02:09, schrieb Julie:
Surely though, there must be some standard design principals behind the use
of sub-classes?

When it boils down, that is my actual question... When to use and when not
to use sub-classes.

Do you know of any information I can look up on this?

The term "sub class" can be misleading. If you search for "nested classes"
(or nested types), you find also the following link which says something
about it's usage:

http://msdn.microsoft.com/en-us/library/ms229027.aspx
 
E

ersatz53

I have a class that has a sub collection class.

Public Class Item
    Public Collection As ItemCollection

    Public Class ItemCollection
        Inherits Generic.List(Of Item)
    End Class
End Class

I have two questions:
1. When is it appropriate to use a sub class like above? I got the idea from
System.Windows.Forms.Control. But when i look at
System.Windows.Forms.TreeNode, it doesn't have its collection class as a sub
class (System.Windows.Forms.TreeNodeCollection). Is there a web site the
defines the best practices for class and subclass usage?

2. If i were to inherit Item into ItemDetail, the ItemCollection wouldn'tbe
type specific for ItemDetail. So what would I do in this case? Create
another sub collection class? I don't think ItemDetailCollection could
inherit ItemCollection.

Balena in his book suggests that subclassing was useful for overcoming
the limitations in the VB6 language. He also claims that with the
power of the Windows Forms classes are so powerful that there is
rarely a need to resort to subclassing. (Balena, Francesco. Programing
Visual Basic .NET. p753. Microsoft Press)

/tr
 
J

Julie

well, i don't know about anybody else, but i think it is a nice way to
structure code (however, i might not understand it completely).

For example, if i had a class called Order, and this class has a lot of
properties, but also has order lines (a collection of products associated
with an order), rather than have two classes, Order and OrderLine, i could
subclass the line, which then doesn't require the Order prefix. That is, i
would have Order, and Order.Line (sub class of order).

You wouldn't have a Line without an Order.

Does this make sense or am i on the wrong track?

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top