VB.Net/Generics/Generic Class
Содержание
Declare a generic type
Public Class Car
End Class
Module Program
Sub Main()
Dim myCars As New List(Of Car)
myCars.Add(New Car())
Console.WriteLine(myCars.Count)
Dim MyInts As New List(Of Integer)
MyInts.Add(50)
Dim val As Integer = MyInts.Count
Console.WriteLine(MyInts.Count)
End Sub
End Module
Generic Class Demo
Public Class MainClass
Public Shared Sub Main()
Dim talker1 As New GenericTalker(Of Dog)()
talker1.Talker = New Dog()
talker1.sayHello()
Dim talker2 As New GenericTalker(Of Cat)()
talker2.Talker = New Cat()
talker2.sayHello()
End Sub
End Class
Public Class GenericTalker(Of T)
Public Talker As T
Public Sub SayHello()
Dim helloWorld As String
helloWorld = Talker.ToString()
Console.WriteLine(helloWorld)
End Sub
End Class
Public Class Dog
Public Overrides Function ToString() As String
Return "Dog!"
End Function
End Class
Public Class Cat
Public Overrides Function ToString() As String
Return "Cat"
End Function
End Class
Public Class Mouse
Public Overrides Function ToString() As String
Return "Mouse"
End Function
End Class
Public Class Person
Public Overrides Function ToString() As String
Return "Person"
End Function
End 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
Generic Point Structure
Option Explicit On
Option Strict On
Module Program
Sub Main()
Dim intPt As New Point(Of Integer)(100, 100)
Console.WriteLine(intPt.ToString())
Dim dblPt As New Point(Of Double)(5.6, 3.23)
Console.WriteLine(dblPt.ToString())
Dim p1 As New Point(Of Integer)(10, 43)
Dim p2 As New Point(Of Integer)(6, 987)
Console.WriteLine(p1)
Console.WriteLine(p2)
Swap(Of Point(Of Integer))(p1, p2)
Swap(p1, p2)
Console.WriteLine(p1)
Console.WriteLine(p2)
End Sub
Public Function Swap(Of T)(ByRef a As T, ByRef b As T) As T
Console.WriteLine("T is a {0}.", GetType(T))
Dim temp As T
temp = a
a = b
b = temp
End Function
End Module
Public Structure Point(Of T)
Private xPos, yPos As T
Public Sub New(ByVal x As T, ByVal y As T)
xPos = x : yPos = y
End Sub
Public Property X() As T
Get
Return xPos
End Get
Set(ByVal value As T)
xPos = value
End Set
End Property
Public Property Y() As T
Get
Return xPos
End Get
Set(ByVal value As T)
yPos = value
End Set
End Property
Public Overrides Function ToString() As String
Return String.Format("({0}, {1})", xPos, yPos)
End Function
End Structure