473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to use enumerable

Art
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
Nov 20 '05 #1
4 7115
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 . . .
Nov 20 '05 #2
CollectionBase already impements IEnumerable

/claes

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:D0**********************************@microsof t.com...
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

Nov 20 '05 #3
Art
Claes,

Thank you very much!

Art
Nov 20 '05 #4
Art
OHM,

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

Art
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
1
by: Christopher Benson-Manica | last post by:
I'd like to add a method foo() to all objects: Object.prototype.foo=function() { // whatever } The caveat, though, is that I do not want foo to be enumerable by a for-in loop. Can I do...
4
by: Michael K. Walter | last post by:
I'd like to create a strongly-typed collection of objects that are indexed by a string value (key is a string). I'd also like to allow users to iterate over the collection using a For-each loop...
15
by: Fabio Cannizzo | last post by:
Is it possible to do something similar to the STL iterators, i.e. to execute a foreach loop in reverse order? Thanks, Fabio
1
by: Hermann Klinke | last post by:
This is what I mean: I have the an enumerable object (implements IEnumerable =GetEnumerator() method) and I would like to find out what types underly this enumerable object. For example, if I have...
1
by: dhtmlkitchen | last post by:
I think this is not possible... var fnProto = Function().prototype; var badAss = { constructor : "Chuck Norris" }; result = ; function isConstructorEnumerable( ocean, pacific ) {
1
by: Blip | last post by:
public class IP_ADAPTER_INFO //: IEnumerable<object<-maybe { public IntPtr Next; public uint ComboIndex; public string AdapterName; ...etc... public IP_ADAPTER_INFO(IntPtr baseAddress) {...
3
by: Tony | last post by:
Hello! Interface IEnumerable consist of method GetEnumerator() which will return an object of type IEnumerator. Now to my question will every object that GetEnumerator return implement...
4
by: Kenneth Porter | last post by:
Can someone point me to documentation or examples on how to implement an Enumerable interface in C++/CLI? I've found examples for C# but nothing for C++. I'm wrapping a native library that...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.