vb.netobject-designobject-composition

vb.net - Object aggregation of inherited classes


I'm playing around with composition of a couple of objects.

I have two classes (Note and task). The Task class is derived from the Note class as a task is an extented note.

Each note has a property Property Child as list (of note) as a note or task could be added to an existing note or task (Therefore this 'child' note could be a task or a note)

ie.

dim x as new note()

x.Child.item(0).Child.item(0).Child.item(0).description.ToString()

the final child note object is actually a task, how can i make this aggregation work? i don't care if its a note or a task but I would like to release the functionality of the base or the extended class.

My immediate thoughts were that each object needs to have a list of tasks and a list of notes but it feels like there could be a more elegant solution.

Does anybody have any thoughts on this?


Solution

  • You're collection of Note objects should already give you what you want.

    If you need to differentiate between Note and Task you can ask if the instance is a 'typeof' Task and then cast appropriately to get at the derived properties and methods.

    If the methods and properties you need are part of the base class, you don't need to cast, you can rely on polymorphism to call the type-appropriate method/property.