vb.net – 创建集合对象

前端之家收集整理的这篇文章主要介绍了vb.net – 创建集合对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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的世界中,这是我非常新的(甚至不能完全确定我是在正确的道路上).假设我有一节课:

  1. Public Class BakedBean
  2. Private Shape As String
  3. Private Variety As String
  4. Private Flavour As String
  5. 'etc
  6. End Class

另一个代表BakedBeans集合的类:

  1. Public Class TinOfBeans
  2. '?
  3. End Class

我希望能够将TinOfBeans类中的Beans作为集合,这样我就可以进行这样的调用,但不依赖于特定的集合类型:

  1. Dim myTin As New TinOfBeans()
  2. myTin.Add(New BakedBean(...))
  3.  
  4. For Each bean As BakedBean in myTin
  5. '...
  6. Next
  7.  
  8. myTin(0).Flavour = "beany"

我一直在关注IEnumerable和IEnumerator,到目前为止我已经有了这么多,但是我已经迷失了它:

BakedBean类

  1. Public Class BakedBean
  2. Private Shape As String
  3. Private Variety As String
  4. Private Flavour As String
  5. 'etc
  6. End Class

BeansEnumerator类

  1. Public Class BeansEnumerator
  2. Implements IEnumerator
  3.  
  4. Private Position As Integer = -1
  5.  
  6. Public ReadOnly Property Current As Object Implements System.Collections.IEnumerator.Current
  7. Get
  8. '???
  9. End Get
  10. End Property
  11.  
  12. Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
  13. '???
  14. End Function
  15.  
  16. Public Sub Reset() Implements System.Collections.IEnumerator.Reset
  17. Position = -1
  18. End Sub
  19. End Class

TinOfBeans类

  1. Public Class TinOfBeans
  2. Implements IEnumerable
  3.  
  4. Private beansEnum As BeansEnumerator
  5.  
  6. Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
  7. Return beansEnum
  8. End Function
  9. End Class

在这一点上,我变得非常紧张,并且不知道如何继续(或者,正如我在开始时说的,如果这是正确的方法).有什么建议?

@H_403_34@
你这么做太难了. .Net已经通过使用泛型提供了所需的确切类.

I want to be able to … make calls like these:

  1. Dim myTin As New TinOfBeans()
  2. myTin.Add(New BakedBean(...))
  3.  
  4. For Each bean As BakedBean in myTin
  5. '...
  6. Next
  7.  
  8. myTin(0).Flavour = "beany"

当然.这是如何做:

  1. Dim myTin As New List(Of BakedBean)()
  2. myTin.Add(New BakedBean(...))
  3.  
  4. For Each bean As BakedBean in myTin
  5. '...
  6. Next
  7.  
  8. myTin(0).Flavour = "beany"

你的每一行都准确映射.让Generic Collections为您完成工作.

您似乎也对IEnumerable感到困惑,我还有一个要求可以实现:

make calls … without being tied to a specific collection type

我们可以用一种技术来涵盖这两种技术.让我们定义一个接受Bean集合的方法

  1. Public Sub CookBeans(ByVal beans As List(Of BakedBean))

但是,这不处理数组,迭代器或其他BakedBean集合类型.这里的答案是IEnumerable:

  1. Public Sub CookBeans(BvVal beans As IEnumerable(Of BakedBean))
  2. For Each bean As BakedBean In beans
  3. Cook(bean)
  4. Next
  5. End Sub

这次我包含了该方法的示例实现,以便您可以看到它的工作方式.这里要理解的重要一点是,此方法允许您调用它并传递Array,List,Iterator或任何其他BakedBean集合.

这是有效的,因为List(Of BakedBean)是(或者更确切地说,实现)IEnumerable(Of BakedBean). BakedBean数组和其他.Net集合也是如此. IEnumerable是所有集合的根接口.你可以在某个地方的内存中有一个具体的List(Of BakedBean)对象,但是使用IEnumerable(Of BakedBean)定义你的方法参数和返回类型,如果你决定将那个List对象更改为其他东西,那些方法仍然可以工作.

您还可以返回IEnumerable:

  1. Public Function GetPintos(ByVal beans As IEnumerable(Of BakedBean)) As IEnumerable(Of BakedBean)
  2. Dim result As IEnumerable(Of BakedBean)
  3. result = beans.Where(Function(bean) bean.Variety = "pinto")
  4. Return result
  5. End Function

如果你需要IEnumerable成为一个列表,它很容易:

  1. Dim beans As List(Of BakedBeans) = '... get beans from somewhere
  2.  
  3. Dim pintos As List(Of BakedBeans) = GetPintos(beans).ToList()
  4. Cook(pintos)

猜你在找的VB相关文章