VB.Net Tutorial/Collections/IEnumerator

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

Enumerator Reset

Imports System.Collections

public class Test
   public Shared Sub Main
        Dim StringArray() As String = {"This", "is", "a", "test"}
        
        Array.Reverse(StringArray)
            
        Dim E As IEnumerator = StringArray.GetEnumerator()
    
        While (E.MoveNext())
          Console.WriteLine(E.Current())
        End While
    
        E.Reset()
    
        While (E.MoveNext())
          Console.WriteLine(E.Current())
        End While
   End Sub
End class
test
a
is
This
test
a
is
This

Get Enumerator from Collection for custom object

Imports System.Collections
public class Test
   public Shared Sub Main
        Dim employees As New Collection
        employees.Add(New Employee("A"))
        employees.Add(New Manager("B"))
        employees.Add(New Employee("C"))
        Dim emp As Employee
        Dim employee_enumerator As IEnumerator
        employee_enumerator = employees.GetEnumerator()
        Do While (employee_enumerator.MoveNext)
            emp = CType(employee_enumerator.Current, Employee)
            Console.WriteLine(emp.Name)
        Loop
   End Sub
End class
Public Class Employee
    Public Name As String
    Public Sub New(ByVal new_name As String)
        Name = new_name
    End Sub
    Public Overridable Function IsManager() As Boolean
        Return False
    End Function
End Class
Public Class Customer
    Public Name As String
    Public Sub New(ByVal new_name As String)
        Name = new_name
    End Sub
End Class
Public Class Manager
    Inherits Employee
    Public Sub New(ByVal new_name As String)
        MyBase.new(new_name)
    End Sub
    Public Overrides Function IsManager() As Boolean
        Return True
    End Function
End Class
A
B
C

Iterator

Imports System.Collections

public class Test
   public Shared Sub Main
        Dim m_employees As New Collection
        m_employees.Add(New employee("A", "F", "F"))
        m_employees.Add(New employee("B", "E", "H"))
        m_employees.Add(New employee("C", "D", "I"))

        Dim emp As employee
        Dim employee_enumerator As IEnumerator
        employee_enumerator = m_employees.GetEnumerator()
        Do While (employee_enumerator.MoveNext)
            emp = CType(employee_enumerator.Current, Employee)
            If emp.FirstName = "Oingo" Then Continue Do
            Console.WriteLine(emp.Title & " " & emp.FirstName & " " & emp.LastName)
        Loop
   End Sub
End class
    
Public Class Employee
    Public FirstName As String
    Public LastName As String
    Public Title As String
    Public Sub New(ByVal first_name As String, ByVal last_name As String, ByVal new_title As String
)
        FirstName = first_name
        LastName = last_name
        Title = new_title
    End Sub
End Class
F A F
H B E
I C D

Iterator with While End

Imports System.Collections

public class Test
   public Shared Sub Main
        Dim m_employees As New Collection
        m_employees.Add(New employee("A", "F", "F"))
        m_employees.Add(New employee("B", "E", "H"))
        m_employees.Add(New employee("C", "D", "I"))
        Dim emp2 As Employee
        Dim employee_enumerator2 As IEnumerator
        employee_enumerator2 = m_employees.GetEnumerator()
        While (employee_enumerator2.MoveNext)
            emp2 = CType(employee_enumerator2.Current, Employee)
            If emp2.FirstName = "A" Then Continue While
            Console.WriteLine(emp2.Title & " " & emp2.FirstName & " " & emp2.LastName)
        End While
   End Sub
End class
    
Public Class Employee
    Public FirstName As String
    Public LastName As String
    Public Title As String
    Public Sub New(ByVal first_name As String, ByVal last_name As String, ByVal new_title As String
)
        FirstName = first_name
        LastName = last_name
        Title = new_title
    End Sub
End Class
H B E
I C D

Loop through an array using the Enumerator

Imports System.Collections
public class Test
   public Shared Sub Main
        Dim StringArray() As String = {"This", "is", "a", "test"}
        Dim E As IEnumerator = StringArray.GetEnumerator()
    
        While (E.MoveNext())
          Console.WriteLine(E.Current())
        End While
    
        Console.WriteLine(Array.IndexOf(StringArray, "test"))
   End Sub
End class
This
is
a
test
3