VB.Net Tutorial/Generics/Generic List

Материал из VB Эксперт
Перейти к: навигация, поиск

Generic list of user objects

<source lang="vbnet">Imports System.Collections.Generic public class Test

  public Shared Sub Main
       Dim emp_list As New EmployeeList
       emp_list.Add(New Employee("G", "M"))
       emp_list.Add("Z", "M")
  End Sub
  

End class Public Class Employee

   Private m_FirstName As String
   Private m_LastName As String
   Public Sub New(ByVal first_name As String, ByVal last_name As String)
       m_FirstName = first_name
       m_LastName = last_name
   End Sub
   Public Overrides Function ToString() As String
       Return m_FirstName & " " & m_LastName
   End Function

End Class Public Class EmployeeList

   Inherits List(Of Employee)
   Public Overloads Sub Add(ByVal first_name As String, ByVal last_name As String)
       Dim emp As New Employee(first_name, last_name)
       MyBase.Add(emp)
   End Sub

End Class</source>

Use generic List

<source lang="vbnet">Imports System.Collections.Generic public class Test

  public Shared Sub Main
       Dim places As New List(Of String)
       places.Add("A")
       places.Add("B")
       places.Add("C")
       places.Add("D")
  End Sub
  

End class</source>