IENUMERATOR interface in VB.NET

xiaoxiao2021-03-05  46

The IEnumerator interface is implemented in object-oriented design, and this object with similar parent is often used. For example, in one of my current projects, there is an order object, in one order contains multiple products, this I want to use Iterator mode to encapsulate the product under orders, and the IEnumerator interface in DOT NET is used to implement iteration to support the operation of the for Each in the DOT NET.

To implement the IEnumerator interface, you need to support the following functions to support the operation of the IEnumerator interface.

OVERRIDABLE READONLY Property Current () AS Object

Current is used to get the current object during iteration

Public overridable function moveXExt () as boolean

MoveNext is used to point the iteration pointer to the next object during iteration, and the initial iteration pointer points to the beginning of the collection (where the first node is positioned), once the end of the collections, before calling the reset, MOVENEXT subsequent calls Return False.

OVERRIDABLE SUB RESET () Sets the enumerated number to its initial position, which is before the first element in the collection.

As long as the collection remains unchanged, the number will remain valid. If the collection is changed (eg, add, modify, or delete an element), the number of the enumeration will be invalid and unrecoverable, and the next time the next time the call will raise InvalidOperationException.

The lower needs is a specific implementation of the Ienumerator interface.

'--------------------------------------------------------------------------------------------------------------------------- ----------------

Imports system.collections

'The actual implementation here is the system.collections.ienumerable interface, and IteratorProduct uses this interface to provide the user to the user's operation.

Public Class IteratorProduct: Implements System.collections.ienumerable Private Products As Collection 'All Products of All Products in Mounts Subscribers Private item as INTEGER = -1

Public Sub New () Products = New Collection Products.Add ("XH") 'This is just for easy testing, and will be written directly in this products.add ("lj") products.add ("qd") End Sub

OVERRIDABLE READENLY Property Current () As Object Get Return Products (Item) End Get End Property

Public overridable function moveXT () as boolean item = 1 End Function

OVERRIDABLE SUB RESET () ITEM = -1 End Sub

'Return iteable to the user

Overridable function genumerator () AS IENUMERATOR IMPLEments IENUMERABLE.GETENUMERATOR RETURN Me.Products.GeteNumerator End Functionend Class

'------------------------ Use the class ----------------------- -----------

Private Sub Page_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Products As IteratorProduct Products = New IteratorProduct Dim ProductName As String For Each ProductName In Products Response.Write (ProductName) Response.Write ( "
") Next End Sub

The output is:

XHLJQD illustrates success

转载请注明原文地址:https://www.9cbs.com/read-31941.html

New Post(0)