Hello,
this
below work but it isn't what I'd like; GetNumber() return an Enumerable
and at the moment I'd like that it return an IEnumerator ...
Can I have an help, please?
class Number {
List
Number (){number=new List
public void insert(int n) { number.Add(n); }
public static IEnumerable<int> GetNumber() {
IEnumerator ir = number.GetEnumerator();
for (; ir.MoveNext(); ){
if (ir.Current.isPair() )
yield return ir.Current;
}
}
}
class Program
{
static void
Number n = new Number();
n.insert (10); n.insert (20); .....30, 40...50
foreach (int i in GetNumber() )
Console.WriteLine("Number {0}", i);
}
}

AlanRank #669 · 3.6K reputationPosted Oct 29, 2007, 11:50 AM EDT
I'm not entirely sure what you're doing here as I can't see a defintion of isPair() anywhere.
However, ignoring that for the moment, this is the sort of pattern you need to return an IEnumerator for the Number class which just iterates through the list of integers:
using System;
using System.Collections;
using System.Collections.Generic;
class Number : IEnumerable
{
List
public Number()();
{
number=new List
}
public void insert(int n){
number.Add(n);
}
public IEnumerator GetEnumerator()
{
return new NumberEnumerator(number);
}
}
class NumberEnumerator : IEnumerator
{
List
int position = -1;
public NumberEnumerator(List number)
{
this.number = number;
}
public bool MoveNext()
{
position++;
return (position < number.Count);
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return number[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
class Program
{
static void Main(string[] args)
{
Number n = new Number();
for (int i = 10; i < 60; i += 10)
{
n.insert (i);
}
foreach (int i in n)
Console.WriteLine("Number {0}", i);
}
}
Marco59 reputationPosted Oct 29, 2007, 1:48 PM EDT
where there was isPair() change to this:
public static IEnumerable<int> GetNumber(int num) {
IEnumerator ir = number.GetEnumerator();
for (; ir.MoveNext(); ){
if (ir.Current> num *2 )
yield return ir.Current;
}
}
}Are you understand? here above I just find the change returned value into IEnumerator....Having to check " if (ir.Current> num *2 )" how can I change your implementation (in a way that after I'll can call the usual number.Movenext() too...
thanks
AlanRank #669 · 3.6K reputationPosted Oct 29, 2007, 4:35 PM EDT
So you only want to iterate through those integers which are more than twice as big as the argument to the GetNumber method. The best I can come up with is this:
using System;
using System.Collections;
using System.Collections.Generic;
class Number : IEnumerable
{
List
public Number()
{
number = new List
}
private Number(List list)
{
number = list;
}
public void insert(int n){
number.Add(n);
}
public IEnumerator GetEnumerator()
{
return new NumberEnumerator(number);
}
public Number GetNumber(int num) temp = new List();
{
List
foreach (int i in number)
{
if (i > num * 2)
temp.Add(i);
}
return new Number(temp);
}
}
class NumberEnumerator : IEnumerator
{
List
int position = -1;
public NumberEnumerator(List number)
{
this.number = number;
}
public bool MoveNext()
{
position++;
return (position < number.Count);
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return number[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
class Program
{
static void Main(string[] args)
{
Number n = new Number();
foreach (int i in n.GetNumber(10))
Console.WriteLine("Number {0}", i);
}
}