Not a homework question, despite the bizarreness of the scenario. I've just substituted the real objects I'm working with to simplify the examples.
This has got me started here, but unsure how to proceed. I'm trying to write a class that contains a collection, and I'm getting lost in the world of IEnumerator
and IEnumerable
, which I'm very new to (and not even entirely sure I'm on the right path). Let's say I have a class:
Public Class BakedBean
Private Shape As String
Private Variety As String
Private Flavour As String
'etc
End Class
And another class to represent a collection of BakedBean
s:
Public Class TinOfBeans
'?
End Class
I want to be able to get Beans
in the TinOfBeans
class as a collection, so that I can make calls like these, but without being tied to a specific collection type:
Dim myTin As New TinOfBeans()
myTin.Add(New BakedBean(...))
For Each bean As BakedBean in myTin
'...
Next
myTin(0).Flavour = "beany"
I've been looking at IEnumerable
and IEnumerator
, and I have this much so far, but I'm getting very lost with it:
BakedBean Class
Public Class BakedBean
Private Shape As String
Private Variety As String
Private Flavour As String
'etc
End Class
BeansEnumerator Class
Public Class BeansEnumerator
Implements IEnumerator
Private Position As Integer = -1
Public ReadOnly Property Current As Object Implements System.Collections.IEnumerator.Current
Get
'???
End Get
End Property
Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
'???
End Function
Public Sub Reset() Implements System.Collections.IEnumerator.Reset
Position = -1
End Sub
End Class
TinOfBeans Class
Public Class TinOfBeans
Implements IEnumerable
Private beansEnum As BeansEnumerator
Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return beansEnum
End Function
End Class
At this point I'm getting rather tied up in knots and have no idea how to proceed (or, as I said at the start, if this is even the right approach). Any suggestions?