Reputation: 32143
I have the following structure:
-PARENT
--CHILD
--CHILD
--CHILD
--CHILD
Pretty straight forward. Now, I need the PARENT class to have a function such as createNewChild(id). The PARENT element has the following that each CHILD must override:
Public MustOverride Function getId() As Integer
Now, is it possible to get a list of all available children of a parent at run time so that I can do this? Sorry if this sounds confusing, I'm having a hard time explaining this.
Basically though, I want to be able to do the following:
Dim nParent as PARENT = PARENT.createNewChild(5)
Any ideas? I'm using VB.net so any .net answers are acceptable. Thanks!
Upvotes: 0
Views: 274
Reputation: 3235
Well, the only thing that I could think of to keep track of the children of your parent class is by creating a list of children upon creation.
Class Parent
Private Shared childList As New List(Of Child)()
Public Sub CreateNewChild(id As Integer)
Dim newChild As New Child(id)
childList.Add(newChild)
Return newChild
End Sub
Public Overridable Function GetID() As Integer
Return 0
End Function
Public Shared Function GetAllChildren() As List(Of Child)
Return childList
End Function
End Class
Class Child Inherits Parent
Private m_ID As Integer
Public Property ID() As Integer
Get
Return m_ID
End Get
Set
m_ID = Value
End Set
End Property
Private Sub New(id As Integer)
Me.ID = id
End Sub
End Class
Sorry about the code, I originally wrote it in C# and used an online converter to convert to VB.
Upvotes: 1