Is there an easy way to move a List <t> object?
I have a WPF form that shows a contact (name, address and status).
The GUI is bound to the CurrentContact object, and they are stored in a List<Contact> .
I want to add buttons below:
+ ----- + + ----- + + ----- + + ----- + | << | | <| | > | | >> | + ----- + + ----- + + ----- + + ----- +
The value is first, previous, next and last.
Is there a simple control or convention for iterating over a list? Or do I need to store currentItemIndex and roll my own?
+4
1 answer
Lists provide random access, so you don’t have to go through them to move from one place to another. In fact, it is probably inefficient for repetition if the list is very long; Imagine that you wanted to, for example, go to the last record from the first.
In any case, your four buttons will be:
- first:
list[0] - previous:
list[currentIndex - 1] - next:
list[currentIndex + 1] - last:
list[list.Count - 1]
+5