How to use enumerable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm still very new to vb.net. I created a class, TableList, that inherits collections.base. I'd like to do a "for each" in another class that instantiates a TableList. I think I need to implement IEnumerable.

The things that I know that I don't know are:
If I implement IEnumerable are there any particular methods that I have to create i my TableList Class.
When I do my "for each", it will be something like For Each x in TableList. What do I dimension x as?

I'd greatly appreciate any help.

Art
 
I just knocked this up quickly to show you. There's lots of real life
implementation missing but enough to practically demonstrate how.

Public Class MClassPlanet
Implements System.Collections.IEnumerable
Implements System.Collections.IEnumerator


Private index As Integer = -1
Private MyFriends() As String = {"Lemma", "Emma", "Gemma"}


Public Function GetEnumerator() As System.Collections.IEnumerator
Implements System.Collections.IEnumerable.GetEnumerator
Return CType(Me, IEnumerator)
End Function

Public ReadOnly Property Current() As Object Implements
System.Collections.IEnumerator.Current
Get
Return MyFriends(index)
End Get
End Property

Public Function MoveNext() As Boolean Implements
System.Collections.IEnumerator.MoveNext
If index = MyFriends.GetUpperBound(0) Then
Return False
Else
index += 1
Return True
End If
End Function

Public Sub Reset() Implements System.Collections.IEnumerator.Reset
index = -1
End Sub
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim mgf As New MClassPlanet
Dim ie As IEnumerator = mgf.GetEnumerator()

While ie.MoveNext
Debug.WriteLine(ie.Current.ToString())
End While
End Sub

HTH

OHM ( Terry Burns )
. . . One-Handed-Man . . .
 
CollectionBase already impements IEnumerable

/claes

Art said:
Hi,

I'm still very new to vb.net. I created a class, TableList, that inherits
collections.base. I'd like to do a "for each" in another class that
instantiates a TableList. I think I need to implement IEnumerable.
The things that I know that I don't know are:
If I implement IEnumerable are there any particular methods that I have to create i my TableList Class.
When I do my "for each", it will be something like For Each x in
TableList. What do I dimension x as?
 
OHM,

Thank you very much -- I'll have to spend a little time reading through your example.

Art
 
Back
Top