Creating a collection object

Not a matter of homework, despite the oddity of the scenario. I just replaced the real objects I work with to simplify the examples.

It made me start here, but I don’t know how to continue. I am trying to write a class containing a collection, and I am lost in the world of IEnumerator and IEnumerable , which I am very new to (and not even sure if I am right track). 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 the BakedBean s collection:

 Public Class TinOfBeans '? End Class 

I want to get Beans in the TinOfBeans class as a collection so that I can make similar calls, but not get attached to a specific type of collection:

 Dim myTin As New TinOfBeans() myTin.Add(New BakedBean(...)) For Each bean As BakedBean in myTin '... Next myTin(0).Flavour = "beany" 

I looked at IEnumerable and IEnumerator , and I have so many so far, but I get really lost:

Class BakedBean

 Public Class BakedBean Private Shape As String Private Variety As String Private Flavour As String 'etc End Class 

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

 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 moment, I become quite attached in the nodes and do not know how to act (or, as I said at the beginning, even if this is the right approach). Any suggestions?

+6
source share
2 answers

You make it too complicated .. Net already provides the exact classes you need using Generics.

I want to be able to ... make these calls:

 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" 

Each of your lines accurately displays. Let Generic Collections do the work for you.

You seem to be confused about IEnumerable too, and I have one requirement left:

make calls ... not tied to a particular type of collection

We can use both methods with the same technique. Let it define a method that accepts a Bean collection:

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

However, this does not handle arrays, iterators, or other types of the BakedBean collection. The answer is here: 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. It’s important to understand that this method will allow you to call it and pass in an array, list, iterator, or any other BakedBean collection.

This works because List(Of BakedBean) (or, rather, implements) IEnumerable(Of BakedBean) . Also an array of BakedBean and other .Net collections. IEnumerable is the root interface for all collections. You may have a specific List (Of BakedBean) object in memory somewhere, but specify your method parameters and return types with IEnumerable(Of BakedBean) , and if you ever decide to change this List object to something else , all of these methods will 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 this 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() Cook(pintos) 
+7
source

I think you can be a little complicated. If you really don't want to learn how to use IEnumerable, you just need to inherit the list.

 Public Class TinOfBeans Inherits List(Of BakedBean) End Class 

Or do you have a list inside tin

 Public Class TinOfBeans Public Property Beans As New List(Of BakedBean) End Class 
+1
source

Source: https://habr.com/ru/post/956938/


All Articles