Uses the enumerator with ReadOnlyCollectionBase : ReadOnlyCollectionBase « Data Structure « VB.Net

Uses the enumerator with ReadOnlyCollectionBase


Imports System
Imports System.Collections

Public Class ROCollection
    Inherits ReadOnlyCollectionBase
    Public Sub New(sourceList As IList)
    End Sub 'New
    Default Public ReadOnly Property Item(index As Integer) As [Object]
            Return InnerList(index)
        End Get
    End Property
    Public Function IndexOf(value As [Object]) As Integer
        Return InnerList.IndexOf(value)
    End Function 
    Public Function Contains(value As [Object]) As Boolean
        Return InnerList.Contains(value)
    End Function 
End Class 

Public Class SamplesCollectionBase

    Public Shared Sub Main()
        Dim myAL As New ArrayList()

        Dim myCol As New ROCollection(myAL)

        Console.WriteLine("Contains yellow: {0}", myCol.Contains("yellow"))
        Console.WriteLine("orange is at index {0}.", myCol.IndexOf("orange"))
    End Sub 'Main

    Public Shared Sub PrintValues2(myCol As ROCollection)
        Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.WriteLine("   {0}", myEnumerator.Current)
        End While
    End Sub 'PrintValues2
End Class  


Related examples in the same category

1.ReadOnlyCollectionBase is abstract base class for a strongly typed non-generic read-only collection.
2.Uses the For Each statement which hides the complexity of the enumerator