Class question

R

Rob Meade

Hi all,

I've been writing some .net classes at work now for a while and was feeling
confident in my approach - until I attended a MS ASP.Net course! :blush:)

Before the course, if I was going to create perhaps something that needed an
"item" object, and also a collection of those items, I'd write 2 classes,
the first for the individual items, the second as a collection class which I
would then add all of the individual items to.

For example..

Public Class Book

Private _title as string
Private _author as string

.....

End Class

Public Class Books

Private _collection as collection

....

End Class

So, perhaps I'd be iterating through a recordset from a database, and as I
iterated creating a new "book" and adding it to "books" - then later on I
may do something with my "books" class, iterate through and pull out the
individual books etc...

On the coures I attended they seemed to wrap all this up in the same class -
now to me that makes sense but I am having some problems trying to do it!

For exampe, there would be a class which also had the collection as part of
it...

Public Class Book

Private _title as string
Private _author as string
Private _collection as collection

...

End Class

Now, as I said, this makes sense to me, ie everything to do with this item
in the one place, plus I'd be writing some code that looked like this...

For Each Book In Books.Collection

Which looks nice :blush:)

My problem is that if the class is an individual item AND a collection, when
I instantiate a new one, I'd be creating an empty collection object for each
book, then later on I'd be creating a new instance of the same object to use
the collection properties of it, adding the individual instance to it (with
the empty collection)...

Can someone advise me on the best approach for this please - I feel like I'm
starting to go around in circles!

Thanks in advance for any help you can offer,

Regards

Rob
 
G

Guest

If you are using .net 2.0, using generic collection would be a good idea to achieve same thing
eg

class Book { ....}

List<Book> b = new List<Book>;
Above will create a strongly typed list of Book instances

HTH
Kalpesh
 
C

Chris R. Timmons

Hi all,

I've been writing some .net classes at work now for a while and
was feeling confident in my approach - until I attended a MS
ASP.Net course! :blush:)

Before the course, if I was going to create perhaps something
that needed an "item" object, and also a collection of those
items, I'd write 2 classes, the first for the individual items,
the second as a collection class which I would then add all of
the individual items to.

For example..

Public Class Book

Private _title as string
Private _author as string

.....

End Class

Public Class Books

Private _collection as collection

....

End Class

So, perhaps I'd be iterating through a recordset from a
database, and as I iterated creating a new "book" and adding it
to "books" - then later on I may do something with my "books"
class, iterate through and pull out the individual books etc...

On the coures I attended they seemed to wrap all this up in the
same class - now to me that makes sense but I am having some
problems trying to do it!

For exampe, there would be a class which also had the collection
as part of it...

Public Class Book

Private _title as string
Private _author as string
Private _collection as collection

...

End Class

Now, as I said, this makes sense to me, ie everything to do with
this item in the one place, plus I'd be writing some code that
looked like this...

For Each Book In Books.Collection

Which looks nice :blush:)

My problem is that if the class is an individual item AND a
collection, when I instantiate a new one, I'd be creating an
empty collection object for each book, then later on I'd be
creating a new instance of the same object to use the collection
properties of it, adding the individual instance to it (with the
empty collection)...

Can someone advise me on the best approach for this please - I
feel like I'm starting to go around in circles!

Thanks in advance for any help you can offer,

Regards

Rob,

Your approach of two separate types (Book and Books) is, IMHO,
correct.

The Book and Books types are clearly defined, whereas a Book type
which contains a collection of Book instances is not. Common sense
tells me that such a type is poorly designed. After all, in the real
world, how could a Book contain other Books?!?

Also, with two separate specialized types it's possible to use
inheritance to create new, even more specialized types, like
MedicalBook and MedicalBooks. The Book type w/ a collection would
not (easily) allow this.

There are times when a type can own a collection of that same type.
Tree-like data structures often use this approach.

Even in this case, though, the technique of having two specialized
types is still the way to go (for the reasons previously mentioned).

Whether or not a type should own a collection of the same type is
going to depend on the behavior of the types involved. E.g. a
TreeNode would contain a TreeNodeCollection, but a Book would not
contain a BookCollection.


Hope this helps.

Chris.
 
R

Rob Meade

...
Your approach of two separate types (Book and Books) is, IMHO,
correct.

Hi Chris, thank you :blush:)
The Book and Books types are clearly defined, whereas a Book type
which contains a collection of Book instances is not. Common sense
tells me that such a type is poorly designed. After all, in the real
world, how could a Book contain other Books?!?

Exactly! The one thing I did take from this on the course though was how
the classes were all about the topic, ie, if its to do with a book - it goes
in the book class, if its to do with a bus, it goes in the bus class -
equally this seemed to make good sense.
Whether or not a type should own a collection of the same type is
going to depend on the behavior of the types involved. E.g. a
TreeNode would contain a TreeNodeCollection, but a Book would not
contain a BookCollection.

Thanks for the insight and explanations Chris - appreciated, for now I will
continue as I have been as this method makes good sense to me :blush:)

Rob
 
R

Rob Meade

If you are using .net 2.0, using generic collection would be a good idea
to achieve same thing
eg

class Book { ....}

List<Book> b = new List<Book>;
Above will create a strongly typed list of Book instances

Hi Kalpesh,

Thanks for the reply - I have just started using .net 2.0 actually, still
finding my feet as things "feel" a bit different - I will take a look into
your suggestion - many thanks :blush:)

Rob
 

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