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

◆ reset()

template<typename type_t >
virtual void xtd::collections::generic::ienumerator< type_t >::reset ( )
pure virtual

Sets the enumerator to its initial position, which is before the first element in the collection.

Exceptions
xtd::invalid_operation_exceptionThe collection was modified after the enumerator was created.
xtd::not_supported_exceptionThe enumerator does not support being reset.
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
If changes are made to the collection, such as adding, modifying, or deleting elements, the behavior of xtd::collections::generic::ienumerator::reset is undefined.
The xtd::collections::generic::ienumerator::reset method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a NotSupportedException.
Notes to Implementers
All calls to xtd::collections::generic::ienumerator::reset() must result in the same state for the enumerator. The preferred implementation is to move the enumerator to the beginning of the collection, before the first element. This invalidates the enumerator if the collection has been modified since the enumerator was created, which is consistent with xtd::collections::generic::ienumerator::move_next() and xtd::collections::generic::ienumerator::current.

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