VB.Net Tutorial/Collections/CollectionBase

Материал из VB Эксперт
Версия от 12:53, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Extends CollectionBase to create custom Collection

Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized
public class Test
  
   public Shared Sub Main
        Dim emp_list As New EmployeeCollection
        emp_list.Add(New Employee("A", "A"))
        emp_list.Add(New Employee("B", "B"))
        emp_list.Add(New Employee("C", "C"))
        emp_list.Add(New Employee("D", "D"))
        emp_list.Add(New Employee("E", "E"))
        For Each emp As Employee In emp_list
            Console.WriteLine(emp.ToString)
        Next emp
   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 EmployeeCollection
    Inherits CollectionBase
    Public Sub Add(ByVal value As Employee)
        List.Add(value)
    End Sub
    Public Function Contains(ByVal value As Employee) As Boolean
        Return List.Contains(value)
    End Function
    Public Function IndexOf(ByVal value As Employee) As Integer
        Return List.IndexOf(value)
    End Function
    Public Sub Insert(ByVal index As Integer, ByVal value As Employee)
        List.Insert(index, value)
    End Sub
    Default Public ReadOnly Property Item(ByVal index As Integer) As Employee
        Get
            Return DirectCast(List.Item(index), Employee)
        End Get
    End Property
    Public Sub Remove(ByVal value As Employee)
        List.Remove(value)
    End Sub
End Class
A A
B B
C C
D D
E E

Inherit CollectionBase to create your own Collection structure

Option Strict On
Imports System.Collections
Public Class SortedStrings
   Inherits CollectionBase
  Public Function Add(ByVal value As String) As Integer
     MyBase.List.Add(value)
     
     Sort()
  End Function
  Default Public Property Item(ByVal index As Integer) As String
     Get
        Return DirectCast(MyBase.List.Item(index), String)
     End Get
     Set(ByVal value As String)
        MyBase.List.Item(index) = value
     End Set
  End Property
  Public Sub Sort
     Dim sArr(Me.Count - 1) As String
     Dim IColl As ICollection = Me
     IColl.CopyTo(sArr,0)
     Array.Sort(sArr)
     Me.Clear
     For Each s As String In sArr
        Me.List.Add(s)
     Next
  End Sub
End Class
Public Module TestSortedStrings
   Public Sub Main()
      Dim sstrings As New SortedStrings()
      sstrings.Add("D")
      sstrings.Add("A")
      sstrings.Add("C")
      sstrings.Add("B")
      sstrings.Add("E")
      
      For Each sstring As String In sstrings
         Console.WriteLine(sstring)
      Next
   End Sub
End Module
A
B
C
D
E