Can somebody give me advice on how to create a recursive version of GetEnumerator()?
The well-known Towers of Hanoi problem (see e.g. http://en.wikipedia.org/wiki/Tower_of_Hanoi) may serve as an example that is comparable to the actual problem I have. A simple algorithm to show all moves for a stacjk of disks of height n is:

``````void MoveTower0 (int n, Needle start, Needle finish, Needle temp)
{
if (n > 0)
{
MoveTower0 (n - 1, start, temp, finish);
Console.WriteLine ("Moving disk from {0} to {1}", start, finish);
MoveTower0 (n - 1, temp, finish, start);
}
}``````

What I actually want to do is set up a class HanoiTowerMoves that implements IEnumerable and that enables me to iterate over all moves as follows:

``foreach (Move m in HanoiTowerMoves) Console.WriteLine (m);``

The first step towards a GetEnumerator() implementation seems to get rid of the MoveTower parameters. This can easily be done by using a stack. I also introduced a class Move that combines the parameters into a single variable.

``````class Move
{
public int N { private set; get; }
public Needle Start { private set; get; }
public Needle Finish { private set; get; }
public Needle Temp { private set; get; }

public Move (int n, Needle start, Needle finish, Needle temp)
{
N = n;
Start = start;
Finish = finish;
Temp = temp;
}

public override string ToString ()
{
return string.Format ("Moving disk from {0} to {1}", Start, Finish);
}
}``````

Now MoveTower can be rewritten as follows:

``````void MoveTower1 ()
{
Move m = varStack.Pop ();

if (m.N > 0)
{
varStack.Push (new Move (m.N - 1, m.Start, m.Temp, m.Finish));
MoveTower1 ();
Console.WriteLine (m);
varStack.Push (new Move (m.N - 1, m.Temp, m.Finish, m.Start));
MoveTower1 ();
}
}``````

This version must be called as follows:

``````varStack.Push (new Move (n, Needle.A, Needle.B, Needle.Temp));
MoveTower1 ();``````

The next step towards an iterable version is to implement the class:

``````class HanoiTowerMoves : IEnumerable<Move>
{
Stack<Move> varStack;
int n; // number of disks

public HanoiTowerMoves (int n)
{
this.n = n;
varStack = new Stack<Move> ();
}

public IEnumerator<Move> GetEnumerator ()
{
[B]// ????????????????????????????[/B]  }

// required by the compiler:
IEnumerator IEnumerable.GetEnumerator ()
{
return GetEnumerator ();
}
}``````

Now the big question to me is: what does the body of GetEnumerator () look like?
Can somebody solve this mystery for me?

The code of Program.cs of the console application I created can be found in the attachment.

So you want the GetEnumerator class to return the enumerator for your internal Stack object, varStack ? I would do this:

``````public IEnumerator<Move> GetEnumerator()
{
if (varStack == null)
return null;
else
return varStack.GetEnumerator();
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.