xtd 0.2.0
Loading...
Searching...
No Matches

◆ move_next()

template<typename type_t >
virtual bool xtd::collections::generic::ienumerator< type_t >::move_next ( )
pure virtual

Advances the enumerator to the next element of the collection.

Returns
true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
Exceptions
xtd::invalid_operation_exceptionThe collection was modified after the enumerator was created.
Examples
The following code example demonstrates the implementation of the IEnumerator interfaces for a custom collection. In this example, xtd::collections::generic::ienumerator::move_next is not explicitly called, but it is implemented to support the use of foreach (for each in Visual Basic). This code example is part of a larger example for the IEnumerator interface.
// When you implement IEnumerable, you must also implement IEnumerator.
class people_enum : public ienumerator<person> {
private:
// Enumerators are positioned before the first element
// until the first move_next() call.
public:
const list<person>& people;
explicit people_enum(const list<person>& people) : people {people} {}
bool move_next() override {
++position_;
return position < people.count();
}
void reset() override {position_ = box_integer<size>::max_value;}
const person& current() const override {
try {
return people[position_];
} catch (const index_out_of_range_exception& e) {
throw invalid_operation_exception {csf_};
}
}
};
Represents a boxed integer object.
Definition box_integer.h:52
Supports a simple iteration over a generic collection.
Definition ienumerator.h:58
size_type count() const noexcept override
Gets the number of elements contained in the xtd::collections::generic::list <type_t>.
Definition list.h:289
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:72
#define csf_
Provides information about the current stack frame.
Definition current_stack_frame.h:30
size_t size
Represents a size of any object in bytes.
Definition size.h:23
Remarks
After an enumerator is created or after the xtd::collections::generic::ienumerator::reset method is called, an enumerator is positioned before the first element of the collection, and the first call to the xtd::collections::generic::ienumerator::move_next method moves the enumerator over the first element of the collection.
If xtd::collections::generic::ienumerator::move_next passes the end of the collection, the enumerator is positioned after the last element in the collection and xtd::collections::generic::ienumerator::move_next returns false. When the enumerator is at this position, subsequent calls to xtd::collections::generic::ienumerator::move_next also return false until xtd::collections::generic::ienumerator::reset is called.
If changes are made to the collection, such as adding, modifying, or deleting elements, the behavior of xtd::collections::generic::ienumerator::move_next is undefined.

Implemented in xtd::collections::generic::enumerator< type_t >.