vb.net 创建集合对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19656999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a collection object
提问by Kai
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.
尽管场景很奇怪,但这不是家庭作业问题。我刚刚替换了我正在使用的真实对象以简化示例。
Thishas 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 IEnumeratorand 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:
这让我从这里开始,但不确定如何继续。我正在尝试编写一个包含集合的类,但我在IEnumeratorand的世界中迷失了方向IEnumerable,我对此很陌生(甚至不完全确定我走在正确的道路上)。假设我有一堂课:
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 BakedBeans:
另一个类来表示BakedBeans的集合:
Public Class TinOfBeans
'?
End Class
I want to be able to get Beansin the TinOfBeansclass as a collection, so that I can make calls like these, but without being tied to a specific collection type:
我希望能够作为一个集合Beans进入TinOfBeans类,这样我就可以进行这样的调用,但又不会被绑定到特定的集合类型:
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 IEnumerableand IEnumerator, and I have this much so far, but I'm getting verylost with it:
我一直在看IEnumerableand IEnumerator,到目前为止我有这么多,但我对它感到非常迷茫:
BakedBean Class
烘焙豆类
Public Class BakedBean
Private Shape As String
Private Variety As String
Private Flavour As String
'etc
End Class
BeansEnumerator 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 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
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?
在这一点上,我越来越束手无策,不知道如何继续(或者,正如我在开始时所说,如果这甚至是正确的方法)。有什么建议?
回答by Joel Coehoorn
You're making this way too hard. .Net already providesthe exactclasses you need, through the use of Generics.
你这样做太难了。.Net已经通过使用泛型提供了您需要的确切类。
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.
您的每一行都准确映射。让 Generic Collections 为您完成这项工作。
You seem to also be confused about IEnumerable, and I have one requirement left to implement:
您似乎也对 感到困惑IEnumerable,而我还有一个要求要实现:
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:
我们可以用一种技术涵盖这两种情况。让我们定义一个接受 Bean 集合的方法:
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:
但是,这不处理数组、迭代器或其他 BakedBean 集合类型。这里的答案是 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.
这次我包含了该方法的示例实现,以便您可以了解它是如何工作的。这里要理解的重要一点是,此方法将允许您调用它并传递一个数组、列表、迭代器或任何其他 BakedBean 集合。
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.
这是有效的,因为 aList(Of BakedBean)是(或者更确切地说,实现了) an IEnumerable(Of BakedBean)。BakedBean 数组和其他 .Net 集合也是如此。IEnumerable 是所有集合的根接口。您可以在内存中的某处拥有一个具体的 List(Of BakedBean) 对象,但使用 定义您的方法参数和返回类型IEnumerable(Of BakedBean),如果您决定将该 List 对象更改为其他对象,这些方法仍然可以工作。
You can also return IEnumerable:
您还可以返回 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:
如果你需要 IEnumerable 成为一个列表,这很容易:
Dim beans As List(Of BakedBeans) = '... get beans from somewhere
Dim pintos As List(Of BakedBeans) = GetPintos(beans).ToList()
Cook(pintos)
回答by the_lotus
I think you might be complicated things a bit. Unless you really want to learn how to use IEnumerable, you just need to inherits a list.
我想你的事情可能有点复杂。除非你真的想学习如何使用 IEnumerable,否则你只需要继承一个列表。
Public Class TinOfBeans
Inherits List(Of BakedBean)
End Class
Or have a list inside the Tin
或者在 Tin 里面有一个列表
Public Class TinOfBeans
Public Property Beans As New List(Of BakedBean)
End Class

