I’m trying to do something here with VB that I guess I’m not understanding how to do it exactly. Sorry I’m not that good at OOP.
I have a number of things I’m creating and they have two values – parent name and child name (yes, actual people!).
So it would be like this:
Public Class Child
Public Property ParentName As String
Public Property ChildName As String
End Class
And then:
Public Class Parent
Public Property ParentName As String
Public Property ChildName() As String
End Class
Then I need to add these to a Parents class where a Parent can have one or more children.
I start by adding a Child. If that child’s Parent name already exists, just add the Child’s name to that parent, but if it doesn’t exist, create a new parent (with that child). Then add all parents to a collection of parents (with their 1 or more children).
A resulting list would look something like this:
Parents:
- Parent: Jonathan Murry
- Child: Carl Murry
- Parent: Kathleen Anderson
- Child: Steven Anderson
- Child: Deborah Anderson
- Child: Thomas Anderson
- Parent: Xu Jing
- Child: Liu Ming
- Child: Liu Ning
(note on the last one – the parent/child last names don’t need to match – in this case, the children take the father’s last name instead of the mother’s, but we don’t list the father).
How would I create these type of classes so that I can add children to a parent, add a parent to parents and then ensure it is querable with something like Linq?
Thx in advance.
Since all of them are persons, perhaps there should instead be a
Personclass. Each person may aMother, aFatherand a collection ofChildren. That way you can create graphs over several generations and with all sorts of constellations.Update
Here is a fuller (and somewhat reworked) example that perhaps can help to shed some light:
The
Personclass, slightly changes (replacedMother/FatherwithParent, made theChildrenproperty `ReadOnly´)And a sample program creating a
Personlist, and posing a LINQ query.