Re: Question about Iteration and forEach



Never say you can't. We are programmers we can do anything!!!
Here is a sample.. It is probably not as simple as you would like. but it does what you want.
I just defined a couple if interfaces; IPeakableEnumerator<T> and IPeakEnumerable<T>. I then created a collection that derives from List<T> that implements the interfaces.
And viola. You now have a Peak() method on an enumerator that will get the next element for you without moving to the next element.


public class Test
{
public static void DoTest()
{
MyList<string> a = new MyList<string>();
a.Add("a");
a.Add("b");
a.Add("c");
a.Add("d");
a.Add("e");

IPeakableEnumerator<string> en = a.GetPeakableEnumerator();
while (en.MoveNext())
{
Console.WriteLine(en.Current);
Console.WriteLine(en.Peak());
}
}
}



public interface IPeakableEnumerator<T> : IEnumerator<T>
{
T Peak();
}
public interface IPeakEnumerable<T>
{
IPeakableEnumerator<T> GetPeakableEnumerator();
}


public class MyList<T> : List<T>, IPeakEnumerable<T>
{
public IPeakableEnumerator<T> GetPeakableEnumerator()
{
return new MyListEnumerator(this);
}

[Serializable, StructLayout(LayoutKind.Sequential)]
public class MyListEnumerator : IPeakableEnumerator<T>, IEnumerator<T>, IDisposable, IEnumerator
{
private MyList<T> list;
private int index;
private T current;
internal MyListEnumerator(MyList<T> list)
{
this.list = list;
this.index = 0;
this.current = default(T);
}

public void Dispose()
{
}

public bool MoveNext()
{
if (this.index < this.list.Count)
{
this.current = this.list[this.index];
this.index++;
return true;
}
this.index = this.list.Count + 1;
this.current = default(T);
return false;
}

public T Current
{
get
{
return this.current;
}
}
object IEnumerator.Current
{
get
{
if ((this.index == 0) || (this.index == (this.list.Count + 1)))
{
throw new InvalidOperationException("Enumeration failed");
}
return this.Current;
}
}
void IEnumerator.Reset()
{
this.index = 0;
}

public T Peak()
{
if (this.index >= this.list.Count)
{
return default(T);
}
return this.list[index];
}
}
}


Jeremy Shovan
http://www.jeremyshovan.com


"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message news:op.ttsigpm68jd0ej@xxxxxxxxxxxxxxxxxxxxxxx
On Mon, 11 Jun 2007 19:50:40 -0700, news.microsoft.com <billgower@xxxxxxxxxxx> wrote:

I am looping through an iteration and I would like to test the next item but
if its not the one that I want how do I put it back so that when my foreach
continues it is in the next iteration?

You can't. The "foreach" statement strictly enumerates one by one through the collection.

If you want the ability to adjust your position within the enumeration, you can usually just use a normal "for" loop with an index to access individual items within the collection. Then you can adjust the index as needed.

I will note that if you are truly enumerating a list, having a need to look at a specific item and then as a result of that inspection reverse course and go back to the previous item is generally a bad sign. It either means that you're not really enumerating the items in the list, and so enumeration semantics aren't appropriate, or you are enumerating the list in a manner indicative of poor design.

Hopefully you're in the former case, and you really don't want a true enumeration, but you should at least consider the possibility of the latter case. :) For more specific advice, you'd have to post the code for the actual loop you're talking about.

Pete

.



Relevant Pages

  • Re: MprAdminInterfaceEnum
    ... > Trying to enumerate interfaces on RRAS. ... the enumeration cannot be continued. ... > public static extern int MprAdminInterfaceEnum( ...
    (microsoft.public.dotnet.framework.interop)
  • Re: My idea for filtering an Enumeration or Iterator
    ... > interfaces in java.util. ... trivial to sub-class it to implement an enumeration for that class. ... If you absolutely need a generic class that includes more than one ...
    (comp.lang.java.programmer)
  • Re: C# - Adding event delegate declarations to interfaces
    ... Do you mean that the declaration of the delegate type is lexically ... They are common in C++ for enumeration ... so nested types are generally only ... Since interfaces can only have public ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: CodeDom foreach
    ... IEnumerate is supported? ... If either of the generic interfaces are implemented, ... returned during enumeration using Type.GetGenericArgumentson the ... "Mubashir Khan" wrote: ...
    (microsoft.public.dotnet.framework)
  • java - public interface - private menthods
    ... warning - I'm more familiar with C++ but I'm learning Java for new project. ... The intent is that methods may be overridden by clients but only the class that defines the interfaces are allowed to call them. ... void YConsumer(Y & y) ... private void FuncOnlyForX(); // javac saya illegal use of private ...
    (comp.lang.java.programmer)