i need something as simple as IEnumerator that also supports some method like MoveBack().
Printable View
i need something as simple as IEnumerator that also supports some method like MoveBack().
The usual way to use an IEnumerator is through the 'foreach' construct
This approach makes it impossible to 'move back'. If you wanted to be able to invoke MoveBack, you'd have to do something like:Code:foreach (MyObject m in list) {
DoStuff ();
}
Where customenumerator is:Code:CustomEnumerator e = (CustomEnumerator) list.GetEnumerator ();
while (e.MoveNext ()) {
MyObject m = e.Current;
DoStuff (m);
if (condition == true)
e.MoveBack ();
}
and you also implement a class which inherits this interface and fulfills the contract and you also implement a list which returns an instance of this custom enumerator when you call 'GetEnumerator'.Code:interface ICustomEnumerator : IEnumerator
{
bool MoveBack ();
}
It's far far easier to use a standard 'for' loop ;)
If you can't index the collection in question, you could always just do:Code:for (int i=0; i < list.Count; i++) {
MyObject m = list[i];
DoStuff (m);
if (condition == true)
i--;
}
List<MyObject> list = new List<MyObject> (existingEnumerable); and so gain indexing on the collection.
thanks
Hi !
In such cases when I need something like that I'm wrapping my list or dictionarry into a small class which allows to move forward and back step by step :wave:
The above example is a bit more complex then needed for your case as I only copied it from a real world project and it allows to search customer adresses by a customers code and also stepping through them in the order they have been added to the list, because this may be different from the order where they are if I'm calling them sorted by customers code. As you see I'm handling the actualPosition number only and then always calling the CurrentItem method. Because adding Items should not change the CurrentItem position _actPosition ( and vice versa) we have a separate counter for adding Items to the class and for the actual Position valueCode:public class ManualExaminationAdresses {
private Dictionary<string, MirrorAdress> _allAdresses;
private Dictionary<int, string> _allCodes;
private int _actPosition = -1;
private int _itemNo = 0;
public ManualExaminationAdresses() {
_actPosition = -1;
_allCodes = new Dictionary<int, string>();
_allAdresses = new Dictionary<string, MirrorAdress>();
}
public void Add(MirrorAdress mirror) {
_allAdresses.Add(mirror.Code, mirror);
_allCodes.Add(_itemNo, mirror.Code);
_itemNo++;
}
public int Count() {
return _allAdresses.Count;
}
/// <summary>
/// Returns the current Item in the collection without moving to another position
/// </summary>
/// <returns></returns>
public MirrorAdress CurrentItem() {
if (_actPosition >= 0 && _actPosition < _allCodes.Count) {
if ( _allCodes.ContainsKey(_actPosition)){
string xcode = _allCodes[_actPosition];
if (_allAdresses.ContainsKey(xcode)){
return _allAdresses[xcode];
}
}
}
return null;
}
public MirrorAdress GetNextItem() {
_actPosition++;
if (_actPosition >= _allCodes.Count - 1) _actPosition = _allCodes.Count - 1;
return CurrentItem();
}
public MirrorAdress GetPreviousItem() {
_actPosition--;
if (_actPosition < 0) _actPosition = 0;
return CurrentItem();
}
public MirrorAdress GetFirstItem() {
_actPosition = 0;
return CurrentItem();
}
public MirrorAdress GetLastItem() {
_actPosition = _allCodes.Count - 1;
return CurrentItem();
}
internal int ActualRecordNum() {
return _actPosition;
}
}
Hope that helps
thanks mutant and jonny; i also can think about N ways of implementation but my question asked about something as simple as IEnumerator with moveback(); i thought that it may exists something like that in .Net Library that i have missed it.
i have some functiones that return IEnumerator i thought that it will be good if they return something with moveBack() possibility also.
I had looked for the same before I did my own :D
ok, now i am sure it does not exists.