sortedpointlist.vb

来自「OOP With Microsoft VB.NET And Csharp Ste」· VB 代码 · 共 69 行

VB
69
字号
Class SortedPointList
    Implements IEnumerable

    Public Sub New()
    End Sub

    Private m_points As New ArrayList()
    Public Sub AddRandomPoints(ByVal howMany As Integer, _
ByVal maximum As Integer)
        m_points.Clear()
        Dim rgen As New System.Random()
        Dim count As Integer
        For count = 0 To howMany - 1
            m_points.Add( _
                New SortablePoint(rgen.Next(maximum), rgen.Next(maximum)))
        Next
        m_points.Sort()
    End Sub


    Private Class PointEnumerator
        Implements IEnumerator

        Dim m_points As ArrayList
        Dim m_position As Integer = -1
        Dim m_initialCount As Integer

        Public Sub New(ByVal points As ArrayList)
            m_points = points
            m_initialCount = points.Count
        End Sub

        Public Sub Reset() Implements System.Collections.IEnumerator.Reset
            m_position = -1
        End Sub

        Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
            If (m_initialCount = m_points.Count) Then
                m_position += 1
                If (m_position >= m_points.Count) Then
                    Return False
                Else
                    Return True
                End If
            Else
                Throw New InvalidOperationException( _
                    "Collection has changed during enumeration.")
            End If
        End Function

        Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current
            Get
                If (m_initialCount <> m_points.Count) Then
                    Throw New InvalidOperationException( _
                        "Collection has changed during enumeration.")
                ElseIf (m_position >= m_points.Count) Then
                    Throw New InvalidOperationException( _
                        "Enumeration value is invalid.")
                Else
                    Return m_points(m_position)
                End If
            End Get
        End Property
    End Class

    Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        Return New PointEnumerator(m_points)
    End Function
End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?