Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a collection object

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 BakedBeans:

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?

like image 593
Kai Avatar asked Oct 29 '13 11:10

Kai


People also ask

What is a collection object?

A Collection object is an ordered set of items that can be referred to as a unit.

How do you define a collection object in Java?

Any group of individual objects which are represented as a single unit is known as the collection of the objects. In Java, a separate framework named the “Collection Framework” has been defined in JDK 1.2 which holds all the collection classes and interface in it. The Collection interface (java. util.


2 Answers

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()
Cook(pintos)
like image 62
Joel Coehoorn Avatar answered Sep 21 '22 12:09

Joel Coehoorn


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.

Public Class TinOfBeans
    Inherits List(Of BakedBean)

End Class

Or have a list inside the Tin

Public Class TinOfBeans
    Public Property Beans As New List(Of BakedBean)

End Class
like image 41
the_lotus Avatar answered Sep 23 '22 12:09

the_lotus