views:

41

answers:

1

I have a WPF form that shows a contact (Name, Address and State).

The GUI is bound to a CurrentContact object and they are stored in a List<Contact>.

I would like to add buttons to the bottom:

+-----+  +-----+  +-----+  +-----+ 
| <<  |  |  <  |  |  >  |  | >>  |
+-----+  +-----+  +-----+  +-----+  

Meaning first, previous, next and last.

Is there an easy control or convention to iterate through the list? Or do I need to store an currentItemIndex and roll my own?

+2  A: 

Lists provide random access, so you don't need to iterate through them to get from one spot to another. In fact, it's probably inefficient to iterate if the list is very long; imagine you wanted to get to the last record from the first one, for example.

In any case, your four buttons would just be:

  • first: list[0]
  • previous: list[currentIndex - 1]
  • next: list[currentIndex + 1]
  • last: list[list.Count - 1]
John Feminella
John is correct in that if you need random access, using an index is 100% the way to go. For purely iterative/cursor access, though (move to start, end, next, back) an enumerator is most likely more efficient since it is written exactly for this purpose.
David Pfeffer
I was hoping to find something similar to an enumerator that took care of the overflow and such for me. This is what I ended up doing however.
Vaccano