|
-
Dec 2nd, 2002, 08:47 AM
#1
Thread Starter
yay gay
implementing GetEnumerator() in a class
how do i implement this?
o tryed the following:
top
PHP Code:
public class devTab : TabPage, System.Collections.IEnumerator
code
PHP Code:
private int _enumerator = 0;
public bool MoveNext()
{
if (this.Items.Count == _enumerator)
{
return false;
}
else
{
_enumerator++;
return true;
}
}
public void Reset()
{
_enumerator = 0;
}
public object Current
{
get
{
return this.Items[_enumerator];
}
}
(this has more code but it doesnt matter here i think)
but it keeps saying that i cannot make a foreach statement because the class doesnt implement the getenumerator
\m/  \m/
-
Dec 2nd, 2002, 01:37 PM
#2
Frenzied Member
You have to implement the IEnumerable interface also. Here this might help you out.
http://www.c-sharpcorner.com/code/20...InnerClass.asp
Dont gain the world and lose your soul
-
Dec 13th, 2002, 08:29 PM
#3
Thread Starter
yay gay
hmm sorry i took too long but i am having problems with this..i can't understand good the example anyone has another example of this?
dont bother making one just for this, if u have one show me up plz, otherwise just forget it lol
\m/  \m/
-
Dec 14th, 2002, 12:59 PM
#4
Hyperactive Member
I wrote this one then took a look at the link previously posted, and essentially it's the same thing, maybe a little clearer.
PHP Code:
using System;
using System.Collections;
namespace EnumerationExample
{
public class MainApp
{
public static void Main(string[] args)
{
Widgets widgets = new Widgets();
widgets.Add("Chris");
widgets.Add("Sally");
//show the foreach support
Console.WriteLine("Iterating collection using 'foreach'");
foreach(Widget widget in widgets)
{
Console.WriteLine(widget.Name);
}
//show GetEnumerator support
Console.WriteLine("Iterating collection using 'GetEnumerator()'");
IEnumerator myWidgetEnumerator = widgets.GetEnumerator();
while (myWidgetEnumerator.MoveNext())
{
Console.WriteLine(((Widget)myWidgetEnumerator.Current).Name);
}
Console.WriteLine("Press <Enter> to exit ...");
Console.ReadLine();
}
}
public class Widget
{
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public Widget(string Name)
{
this.name = Name;
}
public Widget(){}
}
public class Widgets : IEnumerable
{
private ArrayList widgets = new ArrayList();
public IEnumerator GetEnumerator()
{
return new WidgetEnumerator(widgets);
}
protected class WidgetEnumerator : IEnumerator
{
private ArrayList widgets;
private int index;
public WidgetEnumerator(ArrayList widgets)
{
this.widgets = widgets;
this.Reset();
}
public void Reset()
{
this.index = -1;
}
public bool MoveNext()
{
return (++index < widgets.Count);
}
public object Current
{
get
{
if ( (index < 0) || (index >= widgets.Count))
throw new IndexOutOfRangeException();
return widgets[index];
}
}
}
public void Add(Widget w)
{
this.widgets.Add(w);
}
public void Add(string widgetName)
{
Widget w = new Widget(widgetName);
this.widgets.Add(w);
}
}
}
-
Dec 14th, 2002, 04:08 PM
#5
Thread Starter
yay gay
tks now did it
\m/  \m/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|