Home All Groups Group Topic Archive Search About

Class Design with Collection Classes

Author
22 Mar 2010 2:05 AM
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.

Author
22 Mar 2010 8:31 AM
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

Show quoteHide quote
"Julie" <ju***@home.com> wrote in message
news:eAScZQWyKHA.5364@TK2MSFTNGP05.phx.gbl...
> 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.
Author
23 Mar 2010 1:09 AM
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?


Show quoteHide quote
"Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl> wrote in message
news:O70hRoZyKHA.5132@TK2MSFTNGP05.phx.gbl...
> 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
>
> "Julie" <ju***@home.com> wrote in message
> news:eAScZQWyKHA.5364@TK2MSFTNGP05.phx.gbl...
>> 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.
>
Author
23 Mar 2010 1:33 AM
Tom Shelton
On 2010-03-23, Julie <ju***@home.com> wrote:
> 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 :)

--
Tom Shelton
Author
23 Mar 2010 1:53 AM
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



--
Armin
Author
23 Mar 2010 2:47 PM
ersatz53
Show quote Hide quote
On Mar 21, 10:05 pm, "Julie" <ju***@home.com> wrote:

> 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.
>

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
Author
25 Mar 2010 9:55 PM
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.


Show quoteHide quote
"ersatz53" <tim.r***@gmail.com> wrote in message
news:05803239-9ce2-4400-95e5-9246fe1ee5bf@g10g2000yqh.googlegroups.com...
> On Mar 21, 10:05 pm, "Julie" <ju***@home.com> wrote:
>
>> 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.
>>
>
> 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
Author
26 Mar 2010 5:46 PM
Captain Jack
Show quote Hide quote
"Julie" <ju***@home.com> wrote in message
news:uSWZYXGzKHA.5940@TK2MSFTNGP02.phx.gbl...
> 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.

I don't know if there really is a good standard, but what I do is declare
every public class, structure, and enumeration at the same level. If I have
a class that doesn't need to be used anywhere but within one class, I make
it private and define it in there. My main reason is that the autoindenting
doesn't get confusining (it's easier for me to tell which class I'm working
in, especially if it has a lot of code) and declaring variables of a nested
class doesn't get out of hand by being too long to read easily.

The question I always ask myself on this kind of design issue is, "If I get
hit by a bus on my way to work tomorrow, will somebody else be able to
figure out what I was doing, and why?" I think it's most important to be
consistent, and try to have every developer in the shop be doing things the
same way.

--
Jack