本文整理汇总了VB.NET中System.Collections.IEnumerable.GetEnumerator方法的典型用法代码示例。如果您正苦于以下问题:VB.NET IEnumerable.GetEnumerator方法的具体用法?VB.NET IEnumerable.GetEnumerator怎么用?VB.NET IEnumerable.GetEnumerator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.IEnumerable
的用法示例。
在下文中一共展示了IEnumerable.GetEnumerator方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Person
' 导入命名空间
Imports System.Collections
' Simple business object.
Public Class Person
Public Sub New(ByVal fName As String, ByVal lName As String)
Me.firstName = fName
Me.lastName = lName
End Sub
Public firstName As String
Public lastName As String
End Class
' Collection of Person objects, which implements IEnumerable so that
' it can be used with ForEach syntax.
Public Class People
Implements IEnumerable
Private _people() As Person
Public Sub New(ByVal pArray() As Person)
_people = New Person(pArray.Length - 1) {}
Dim i As Integer
For i = 0 To pArray.Length - 1
_people(i) = pArray(i)
Next i
End Sub
' Implementation of GetEnumerator.
Public Function GetEnumerator() As IEnumerator _
Implements IEnumerable.GetEnumerator
Return New PeopleEnum(_people)
End Function
End Class
' When you implement IEnumerable, you must also implement IEnumerator.
Public Class PeopleEnum
Implements IEnumerator
Public _people() As Person
' Enumerators are positioned before the first element
' until the first MoveNext() call.
Dim position As Integer = -1
Public Sub New(ByVal list() As Person)
_people = list
End Sub
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
position = position + 1
Return (position < _people.Length)
End Function
Public Sub Reset() Implements IEnumerator.Reset
position = -1
End Sub
Public ReadOnly Property Current() As Object Implements IEnumerator.Current
Get
Try
Return _people(position)
Catch ex As IndexOutOfRangeException
Throw New InvalidOperationException()
End Try
End Get
End Property
End Class
Class App
Shared Sub Main()
Dim peopleArray() As Person = { _
New Person("John", "Smith"), _
New Person("Jim", "Johnson"), _
New Person("Sue", "Rabon")}
Dim peopleList As New People(peopleArray)
Dim p As Person
For Each p In peopleList
Console.WriteLine(p.firstName + " " + p.lastName)
Next
End Sub
End Class
输出:
John Smith Jim Johnson Sue Rabon
示例2: MainClass
' 导入命名空间
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main()
Dim intQueue As New Queue( )
Dim i As Integer
For i = 0 To 4
intQueue.Enqueue((i * 5))
Next i
Console.WriteLine("intQueue values:")
DisplayValues(intQueue)
Console.WriteLine("(Dequeue) {0}", intQueue.Dequeue( ))
Console.WriteLine("intQueue values:")
DisplayValues(intQueue)
Console.WriteLine("(Dequeue) {0}", intQueue.Dequeue( ))
Console.WriteLine("intQueue values:")
DisplayValues(intQueue)
Console.WriteLine("(Peek) {0}", intQueue.Peek( ))
Console.WriteLine("intQueue values:")
DisplayValues(intQueue)
End Sub
Public Shared Sub DisplayValues(ByVal myCollection As IEnumerable)
Dim myEnumerator As IEnumerator = myCollection.GetEnumerator( )
While myEnumerator.MoveNext( )
Console.WriteLine("{0} ", myEnumerator.Current)
End While
Console.WriteLine( )
End Sub
End Class