OOP in VB.NET - Child, Parent, Parents

I am trying to do something here with VB, I think I do not understand how to do this. Sorry, I'm not so good at OOP.

I have a few things that I create, and they have two meanings: parent name and child name (yes, actual people!).

So it will 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 them to the class Parentswhere the parent can have one or more children.

I start by adding Baby. If this parent child name already exists, simply add the child name to this parent, but if it does not exist, create a new parent (with this child). Then add all parents to the parents collection (with their 1 or more children).

The resulting list will look something like this:

Parents:

  • Parent: Jonathan Murray
    1. Child: Karl Merry
Parent: Kathleen Anderson
  1. Child: Stephen Anderson
  2. Baby: Deborah Anderson
  3. Child: Thomas Anderson
Parent: Xu Jing
  1. Child: Liu Ming
  2. Child: Liu Ning

(pay attention to the last: the names of the parents / children should not coincide - in this case, the children take the name of the father instead of the mother, but we do not indicate the father).

How do I create these class types so that I can add children to the parent, add the parent to the parents, and then guarantee that it will be queried using something like Linq?

thanks in advance.

+5
source share
1 answer

, , Person. Mother, a Father Children. , .

Public Class Person
    Public Property Name As String
    Public Property Mother As Person
    Public Property Father As Person
    Public Property Children As List(Of Person)
End Class


( ) , , , :

Person, ( Mother/Father Parent, Children `ReadOnly ')

Public Class Person
    Public Property Name As String
    Public Property Parent As Person
    Private _children As New List(Of Person)

    Public ReadOnly Property Children As List(Of Person)
        Get
            Return _children
        End Get
    End Property

End Class

, Person LINQ.

Module Program
    Sub Main()
        Dim persons As IEnumerable(Of Person) = GetPersonGraph()
        Dim jonathansChildren As IEnumerable(Of Person)
        jonathansChildren = persons.Where(Function(p) _
                                Not p.Parent Is Nothing _
                                AndAlso p.Parent.Name = "Jonathan Murphy")

        For Each child As Person In jonathansChildren
            Console.WriteLine(child)
        Next
    End Sub


    Function GetPersonGraph() As IEnumerable(Of Person)
        Dim result As New List(Of Person)
        Dim parent As Person
        Dim child As Person

        parent = New Person()
        parent.Name = "Jonathan Murphy"
        result.Add(parent)

        child = New Person()
        child.Name = "Carl Murry"
        child.Parent = parent
        parent.Children.Add(child)
        result.Add(child)

        Return result
    End Function
End Module
+6

All Articles