Uses the For Each statement which hides the complexity of the enumerator
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) PrintValues1(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 PrintValues1(myCol As ROCollection) Dim obj As [Object] For Each obj In myCol Console.WriteLine(" {0}", obj) Next obj Console.WriteLine() End Sub 'PrintValues1 End Class
1. | ReadOnlyCollectionBase is abstract base class for a strongly typed non-generic read-only collection. | ||
2. | Uses the enumerator with ReadOnlyCollectionBase |