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让我从这里开始,但不确定如何继续.我正在尝试编写一个包含集合的类,并且我迷失在IEnumerator和IEnumerable的世界中,这是我非常新的(甚至不能完全确定我是在正确的道路上).假设我有一节课:
Public Class BakedBean Private Shape As String Private Variety As String Private Flavour As String 'etc End Class
另一个代表BakedBeans集合的类:
Public Class TinOfBeans '? End Class
我希望能够将TinOfBeans类中的Beans作为集合,这样我就可以进行这样的调用,但不依赖于特定的集合类型:
Dim myTin As New TinOfBeans() myTin.Add(New BakedBean(...)) For Each bean As BakedBean in myTin '... Next myTin(0).Flavour = "beany"
我一直在关注IEnumerable和IEnumerator,到目前为止我已经有了这么多,但是我已经迷失了它:
BakedBean类
Public Class BakedBean Private Shape As String Private Variety As String Private Flavour As String 'etc End Class
BeansEnumerator类
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类
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
在这一点上,我变得非常紧张,并且不知道如何继续(或者,正如我在开始时说的,如果这是正确的方法).有什么建议?
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"
当然.这是如何做:
Dim myTin As New List(Of BakedBean)() myTin.Add(New BakedBean(...)) For Each bean As BakedBean in myTin '... Next myTin(0).Flavour = "beany"
你的每一行都准确映射.让Generic Collections为您完成工作.
您似乎也对IEnumerable感到困惑,我还有一个要求可以实现:
make calls … without being tied to a specific collection type
我们可以用一种技术来涵盖这两种技术.让我们定义一个接受Bean集合的方法:
Public Sub CookBeans(ByVal beans As List(Of BakedBean))
但是,这不处理数组,迭代器或其他BakedBean集合类型.这里的答案是IEnumerable:
Public Sub CookBeans(BvVal beans As IEnumerable(Of BakedBean)) For Each bean As BakedBean In beans Cook(bean) Next End Sub
这次我包含了该方法的示例实现,以便您可以看到它的工作方式.这里要理解的重要一点是,此方法允许您调用它并传递Array,List,Iterator或任何其他BakedBean集合.
这是有效的,因为List(Of BakedBean)是(或者更确切地说,实现)IEnumerable(Of BakedBean). BakedBean数组和其他.Net集合也是如此. IEnumerable是所有集合的根接口.你可以在某个地方的内存中有一个具体的List(Of BakedBean)对象,但是使用IEnumerable(Of BakedBean)定义你的方法参数和返回类型,如果你决定将那个List对象更改为其他东西,那些方法仍然可以工作.
您还可以返回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
如果你需要IEnumerable成为一个列表,它很容易:
Dim beans As List(Of BakedBeans) = '... get beans from somewhere Dim pintos As List(Of BakedBeans) = GetPintos(beans).ToList() Cook(pintos)