Translating from C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Woods

    #1

    Translating from C#

    Hi

    For my learning, I'm trying to translate this piece of C# rather than
    use a translator which I don't think will help me on the bit I'm after.

    (http://blogs.msdn.com/ericlippert/ar...ble-stack.aspx)

    public interface IStack<T: IEnumerable<T>
    {
    IStack<TPush(T value);
    IStack<TPop();
    T Peek();
    bool IsEmpty { get; }
    }

    public sealed class Stack<T: IStack<T>
    {
    private sealed class EmptyStack : IStack<T>
    {
    public bool IsEmpty { get { return true; } }
    public T Peek() { throw new Exception("Empt y stack"); }
    public IStack<TPush(T value) { return new Stack<T>(value,
    this); }
    public IStack<TPop() { throw new Exception("Empt y stack"); }
    public IEnumerator<TGe tEnumerator() { yield break; }
    IEnumerator IEnumerable.Get Enumerator() { return
    this.GetEnumera tor(); }
    }
    private static readonly EmptyStack empty = new EmptyStack();
    public static IStack<TEmpty { get { return empty; } }
    private readonly T head;
    private readonly IStack<Ttail;
    private Stack(T head, IStack<Ttail)
    {
    this.head = head;
    this.tail = tail;
    }
    public bool IsEmpty { get { return false; } }
    public T Peek() { return head; }
    public IStack<TPop() { return tail; }
    public IStack<TPush(T value) { return new Stack<T>(value,
    this); }
    public IEnumerator<TGe tEnumerator()
    {
    for(IStack<Tsta ck = this; !stack.IsEmpty ; stack =
    stack.Pop())
    yield return stack.Peek();
    }
    IEnumerator IEnumerable.Get Enumerator() {return
    this.GetEnumera tor();}
    }

    I'm getting a bit stuck on how to translate these lines given what VB
    automatically inserts for me

    public IEnumerator<TGe tEnumerator()
    {
    for(IStack<Tsta ck = this; !stack.IsEmpty ; stack =
    stack.Pop())
    yield return stack.Peek();
    }
    IEnumerator IEnumerable.Get Enumerator() {return
    this.GetEnumera tor();}

    I wonder if someone could explain what needs to go in. I've also read
    elsewhere that VB doesn't support yield return and that you have to
    maintain the state of the enum your self. Can I not do this this
    generically?

    thx

    Simon
Working...