Uses the enumerator with ReadOnlyCollectionBase
Imports System Imports System.Collections Public Class ROCollection Inherits ReadOnlyCollectionBase Public Sub New(sourceList As IList) InnerList.AddRange(sourceList) End Sub 'New Default Public ReadOnly Property Item(index As Integer) As [Object] Get 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() myAL.Add("A") myAL.Add("B") myAL.Add("C") Dim myCol As New ROCollection(myAL) PrintValues2(myCol) 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 Console.WriteLine() End Sub 'PrintValues2 End Class
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 |