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'.
It's far far easier to use a standard 'for' loop
Code:
for (int i=0; i < list.Count; i++) {
MyObject m = list[i];
DoStuff (m);
if (condition == true)
i--;
}
If you can't index the collection in question, you could always just do:
List<MyObject> list = new List<MyObject> (existingEnumerable); and so gain indexing on the collection.
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
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
Code:
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;
}
}
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 value
Hope that helps
Last edited by JonnyPoet; February 8th, 2009 at 05:54 PM.
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ? My latest articles : Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
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.
Please rate my post if it was helpful for you.
C#, C++, PHP, ASP.NET
SQL Server, MySQL
DirectX
MATH
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ? My latest articles : Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Bookmarks