I’m sorry, I didn't mean to offend you! But.
1) If you're going to make a "peekable" enumerator, you should probably
spell the word "peek" correctly. At least, if you expect anyone else to use
it, anyway. :)
--- I will give you this one. But I did write this at 2:00 in the morning.
2) More seriously, you'll note that doing this you have to implement
the entire collection class yourself. There was nothing in the original
post that suggested he was looking to do that, nor is it clear to me that
doing so is always going to be feasible.
--- I did not implement the entire collection
public class MyList<T: List<T>, IPeakEnumerable<T>
{
public IPeakableEnumerator<TGetPeakableEnumerator()
{
return new MyListEnumerator(this);
}
This is hardly the entire collection. This is what we call inheritance.
I did however implement the enumerator as an inner class. But that really is
not a lot of code.
3) It's not at all clear to me that simply "peeking" at the next
element in the iteration would be sufficient for the original poster. I got
the impression he wanted to be able to actually move backwards in the
enumeration.
--- The original poster said "I would like to test the next item but if it’s
not the one that I want how do I put it back so that when my foreach
continues it is in the next iteration".
The key parts to this sentence are:
- "I would like to test the next item
- when my foreach continues it is in the next iteration.
My implementation supports exactly this.
4) I note that the code you've provided doesn't actually use the
"foreach" statement. Again, there seems to be a disconnect between the
original question and the solution you've offered. :)
--- Whether you use a 'while' or a 'foreach' you are still enumerating
through the collection. It really doesn't matter which one you use. There is
nothing saying that you must use a foreach rather than a while.
Also, if you used my implementation in a foreach then you would still be
returned an IEnumerator and that is not the type that I want with my
solution. I want an IPeekableEnumerator
5) Finally, while I admire your dedication to the problem, it seems to
me that it would be simpler to simply emulate a "peekable" enumerator
through something like this (assuming, of course, that being able to "peek"
ahead would suffice for the OP's needs):
--- If you did this it wouldn't be an enumerator, now would it?
Lastly, if you spent this much effort trying to actually come up with
solutions rather than knocking others for actually trying to solve a problem
you would be much more useful and much more liked. Though my solution is not
as good as the one Freddy Potargent provided, at least it is a solution.
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 12 Jun 2007 00:48:01 -0700, Jeremy Shovan
<je***********@student.neumont.eduwrote:
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.
A few thoughts:
1) If you're going to make a "peekable" enumerator, you should
probably spell the word "peek" correctly. At least, if you expect anyone
else to use it, anyway. :)
2) More seriously, you'll note that doing this you have to implement
the entire collection class yourself. There was nothing in the original
post that suggested he was looking to do that, nor is it clear to me that
doing so is always going to be feasible.
3) It's not at all clear to me that simply "peeking" at the next
element in the iteration would be sufficient for the original poster. I
got the impression he wanted to be able to actually move backwards in the
enumeration.
4) I note that the code you've provided doesn't actually use the
"foreach" statement. Again, there seems to be a disconnect between the
original question and the solution you've offered. :)
5) Finally, while I admire your dedication to the problem, it seems to
me that it would be simpler to simply emulate a "peekable" enumerator
through something like this (assuming, of course, that being able to
"peek" ahead would suffice for the OP's needs):
Item itemPrev = null;
foreach (Item itemCur in MyCollection)
{
if (itemPrev != null)
{
// Do some stuff with itemPrev.
// If we want to "peek" at the next one, look at itemCur
}
itemPrev = itemCur;
}
// Finish up by doing stuff with itemPrev here too. No peeking
allowed!
Note that the above works with any class that implements IEnumerable,
which IMHO would be a major benefit over requiring one's collection class
to be entirely redefined just to support that one aspect of the operation.
My two cents. :)
Pete