Vb. Implement IEnumerator interface in net

Source: Internet
Author: User

Vb. Implement IEnumerator interface in net
In object-oriented design, it is often used to have a parent-child relationship, such as in my current project, there are order objects, under an order contains more than one product, then I want to use iterator mode to encapsulate the product under the order, the DOT NET the IEnumerator interface is used to implement iterations to support the operation of the for each in dot net.

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

Overridable ReadOnly Property Current () as Object

Current is used to get the present object in an iterative process


Public Overridable Function MoveNext () as Boolean

MoveNext is used to point the iteration pointer to the next object in the iteration, initially with the iteration pointer pointing to the beginning of the collection (before the first node), and returning false to the subsequent call to MoveNext before the Reset is invoked, once the end of the collection is passed.

Overridable Sub Reset ()
Sets the enumerator to its initial position, which is before the first element in the collection.

The enumerator remains in effect as long as the collection remains unchanged. If you make changes to the collection, such as adding, modifying, or deleting elements, the enumerator will be invalidated and unrecoverable, and the next call to MoveNext or Reset will throw a InvalidOperationException.

The next need is a concrete implementation of the IEnumerator interface to the image

'------------------------the class that implements the IEnumerator interface----------------------------------

Imports System.Collections

' What is actually implemented here is the System.Collections.IEnumerable interface, iteratorproduct with this interface to provide the user with an operation on the IEnumerator interface.

Public Class iteratorproduct:implements System.Collections.IEnumerable
Private products as Collection ' with Collection all product in the deposit order
Private Item as Integer =-1

Public Sub New ()
Products = New Collection
Products.add ("XH") ' This is just for the convenience of testing, will be added to the content of the product directly written in this
Products.add ("LJ")
Products.add ("QD")
End Sub

Overridable ReadOnly Property Current () as Object
Get
Return Products (item)
End Get
End Property

Public Overridable Function MoveNext () as Boolean
Item + 1
End Function

Overridable Sub Reset ()
item =-1
End Sub

' Returns an iterative pair of image to the consumer

Overridable Function GetEnumerator () as IEnumerator Implements Ienumerable.getenumerator
Return Me.Products.GetEnumerator
End Function


End Class



'------------------------use 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
Response.Write (ProductName)
Response.Write ("<br>")
Next
End Sub

The output is:

Xh
Lj
Qd
Description of Implementation Success


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.