School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,109 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,009 people online right now. Registration is fast and FREE... Join Now!




Collections, IEnumerable & IEnumerator (Part 2)

 
Reply to this topicStart new topic

> Collections, IEnumerable & IEnumerator (Part 2)

AdamSpeight2008
Group Icon



post 20 Oct, 2009 - 06:36 PM
Post #1


Continuation from Part 1
Collections, IEnumerable & IEnumerator (Part 2)

What's an Enumerable?
An Enumerable is a class that implements the IEnumerable interface
The IEnumerable
CODE

Public Class Example
Implements IEnumerable

Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator

End Function
End Class

That function returns an Enumemator for the class.


What's an Enumerator
An Enumerator is a class that implements the IEnumerator Interface. It more detail an Enumerator is a method of iterating through the values of a collection.
CODE

Public Class IEnumerator_Example
Implements IEnumerator

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

End Sub
End Class


So you can see why you the error (shown in Part 1)

Example of a class that uses IEnumerable and IEnumerator
CODE

Public Class MyStrings
Implements IEnumerator, IEnumerable

Private mb As String = {", "B", "C", "D"}
Private i As Integer = -1
Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
  Return CType(Me, IEnumerator)
End Function

Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current
  Get
   If i < mb.Count Then
    Return mb(i)
   Else
    Return Nothing
   End If
  End Get
End Property

Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
  If i < mb.Count Then
   i += 1
   Return True
  Else
   Return False
  End If

End Function

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

End Class


Example Usage
CODE

Module Module1

Sub Main()
  Dim bl As New MyStrings
  For Each a In bl
   Console.WriteLine(a)

  Next
End Sub
End Module


Edit: Added link to Part 1.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 12:57PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month