Prev | Current Page 90 | Next

Guy Fouch?©, Trey Nash

"Accelerated VB 2008"

IEnumerator.MoveNext
CHAPTER 3 n CLASSES AND STRUCTURES 44
Position += 1
Return (Position < Drawing.Shapes.Count)
End Function
Public ReadOnly Property Current() As Object _
Implements Collections.IEnumerator.Current
Get
Return Drawing.Shapes(Position)
End Get
End Property
End Class
End Class
Public Class EntryPoint
Shared Sub Main()
Dim Rectangle As New Rectangle
Dim Circle As New Circle
Dim Drawing As New Drawing
Drawing.Add(Rectangle)
Drawing.Add(Circle)
For Each Shape As GeometricShape In Drawing
Shape.Draw()
Next
End Sub
End Class
This example introduces a few new concepts, such as the IEnumerable and IEnumerator
interfaces, which Chapter 10 covers. For now, let??™s focus primarily on the nested class usage. As
you can see, the Drawing class supports a method called GetEnumerator(), which is part of the
IEnumerable implementation. It creates an instance of the nested Iterator class and returns it.
The Iterator class takes a reference to an instance of the containing class, Drawing, as a
parameter to its constructor. It then stores away this instance for later use so that it can get at
the Shapes collection within the Drawing object. However, the Shapes collection in the Drawing
class is Private. It doesn??™t matter, since nested classes have access to the containing class??™s
private members.


Pages:
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
dieta light katalog stron Tango Olsztyn wierszyki katalog stron