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?
You're making this way too hard. .Net already provides the exact classes you need, through the use of Generics.
I want to be able to ... make calls like these:
Dim myTin As New TinOfBeans()
myTin.Add(New BakedBean(...))
For Each bean As BakedBean in myTin
'...
Next
myTin(0).Flavour = "beany"
Sure. Here's how:
Dim myTin As New List(Of BakedBean)()
myTin.Add(New BakedBean(...))
For Each bean As BakedBean in myTin
'...
Next
myTin(0).Flavour = "beany"
Every one of your lines maps exactly. Let Generic Collections do the work for you.
You seem to also be confused about IEnumerable
, and I have one requirement left to implement:
make calls ... without being tied to a specific collection type
We can cover both of those with one technique. Let's define a method that accepts a Bean collection:
Public Sub CookBeans(ByVal beans As List(Of BakedBean))
However, this does not handle arrays, or Iterators, or other BakedBean collection types. The answer here is IEnumerable:
Public Sub CookBeans(BvVal beans As IEnumerable(Of BakedBean))
For Each bean As BakedBean In beans
Cook(bean)
Next
End Sub
This time I included an example implementation of the method, so that you can see how it will work. The important thing to understand here is that this method will allow you to call it and pass an Array, List, Iterator, or any other BakedBean collection.
This works because a List(Of BakedBean)
is (or rather, implements) an IEnumerable(Of BakedBean)
. So does a BakedBean array and other .Net collections. IEnumerable is the root interface for all collections. You can have a concrete List(Of BakedBean) object in memory somewhere, but define your method parameters and return types using IEnumerable(Of BakedBean)
, and if you ever decide to change that List object to something else, those methods will all still work.
You can also return IEnumerable:
Public Function GetPintos(ByVal beans As IEnumerable(Of BakedBean)) As IEnumerable(Of BakedBean)
Dim result As IEnumerable(Of BakedBean)
result = beans.Where(Function(bean) bean.Variety = "pinto")
Return result
End Function
And if you ever need that IEnumerable to become a list, it's pretty easy:
Dim beans As List(Of BakedBeans) = '... get beans from somewhere
Dim pintos As List(Of BakedBeans) = GetPintos(beans).ToList()
CookBeans(pintos)