Generic List stores Generic Class : Generic Class « Generics « VB.Net






Generic List stores Generic Class

Generic List stores Generic Class
 
Imports System.Collections
Imports System.Collections.Generic

Public Class MainClass

   Public Shared Sub Main()
        Dim custColl As New List(Of Customer)()
        Dim cust As New Customer("1", "Name")
        cust.AddItem(New Order(DateTime.Parse("10/01/2004"), "1", "N"))
        cust.AddItem(New Order(DateTime.Parse("10/03/2004"), "2", "X"))
        cust.AddItem(New Order(DateTime.Parse("10/07/2004"), "3", "P"))

        custColl.Add(cust)

        cust = New Customer("2", "name 1")
        cust.AddItem(New Order(DateTime.Parse("11/04/2004"), "6", "P"))
        cust.AddItem(New Order(DateTime.Parse("11/07/2004"), "8", "Y"))
        cust.AddItem(New Order(DateTime.Parse("12/12/2004"), "9", "K"))

        custColl.Add(cust)

        cust = New Customer("3", "Name 3")
        cust.AddItem(New Order(DateTime.Parse("10/01/2004"), "4", "W"))

        custColl.Add(cust)


        For custIdx As Int32 = 0 To (custColl.Count - 1)
            cust = custColl(custIdx)
            Console.Out.WriteLine("Customer-) ID: {0}, Name: {1}", cust.Id, cust.Name)

            Dim orders() As Order = cust.Items
            For orderIdx As Int32 = 0 To (orders.Length - 1)
                Dim ord As Order = orders(orderIdx)
                Console.Out.WriteLine("    Order-> Date: {0}, Item: {1}, Desc: {2}", ord.OrderDate, ord.ItemId, ord.Description)
            Next
        Next



        Dim empColl As New List(Of Employee)()
        Dim emp As New Employee("1", "R")
        empColl.Add(emp)

        emp = New Employee("2", "M")
        empColl.Add(emp)

        emp = New Employee("3", "B")
        emp.AddItem(New Employee("6", "S"))
        emp.AddItem(New Employee("7", "B"))
        emp.AddItem(New Employee("8", "T"))

        empColl.Add(emp)

        For mgrIdx As Int32 = 0 To (empColl.Count - 1)
            Dim manager As Employee = empColl(mgrIdx)
            Console.Out.WriteLine("Manager-) ID: {0}, Name: {1}", manager.Id, manager.Name)

            For idx As Int32 = 0 To (manager.Items.Length - 1)
                emp = manager.Items(idx)
                Console.Out.WriteLine("    Employee-) Id: {0}, Name: {1}", emp.Id, emp.Name)
            Next
        Next
   End Sub
End Class

Public Class Customer
    Inherits Person(Of Order)

    Public Sub New(ByVal Id As String, ByVal Name As String)
        MyBase.New(Id, Name)
    End Sub

End Class


Public Class Employee
    Inherits Person(Of Employee)

    Public Sub New(ByVal Id As String, ByVal Name As String)
        MyBase.New(Id, Name)
    End Sub

End Class


Public Class Order
    Public OrderDate As DateTime
    Public ItemId As String
    Public Description As String

    Public Sub New(ByVal OrderDate As DateTime, ByVal ItemId As String, ByVal Description As String)
        Me.OrderDate = OrderDate
        Me.ItemId = ItemId
        Me.Description = Description
    End Sub

End Class


Public Class Person(Of T)
    Public Name As String
    Public Id As String
    Private _items As List(Of T)

    Public Sub New(ByVal Id As String, ByVal Name As String)
        Me.Id = Id
        Me.Name = Name
        Me._items = New List(Of T)
    End Sub

    Public ReadOnly Property Items() As T()
        Get
            Return Me._items.ToArray()
        End Get
    End Property

    Public Sub AddItem(ByVal newItem As T)
        Me._items.Add(newItem)
    End Sub

End Class

           
         
  








Related examples in the same category

1.Generic Class DemoGeneric Class Demo
2.Declare a generic type
3.Generic Point Structure