Inventing The Wheel, Simple Practice; Implementing IList (Of T)

Public Class RiverNileList(Of T)
    Implements IList(Of T)

    Private _list As IList

    Public Sub New(list As IList)
        _list = list
    End Sub

    Public ReadOnly Property Count As Integer Implements ICollection(Of T).Count
        Get
            Return _list.Count
        End Get
    End Property

    Public ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of T).IsReadOnly
        Get
            Return _list.IsReadOnly
        End Get
    End Property

    Default Public Property Item(index As Integer) As T Implements IList(Of T).Item
        Get
            Return CType((CObj(_list(index))), T)
        End Get
        Set(value As T)
            _list(index) = value
        End Set
    End Property

    Public Sub Add(item As T) Implements ICollection(Of T).Add
        _list.Add(item)
    End Sub

    Public Sub Clear() Implements ICollection(Of T).Clear
        _list.Clear()
    End Sub

    Public Sub CopyTo(array() As T, arrayIndex As Integer) Implements ICollection(Of T).CopyTo
        _list.CopyTo(array, arrayIndex)
    End Sub

    Public Sub Insert(index As Integer, item As T) Implements IList(Of T).Insert
        _list.Insert(index, item)
    End Sub

    Public Sub RemoveAt(index As Integer) Implements IList(Of T).RemoveAt
        _list.RemoveAt(index)
    End Sub

    Public Function Contains(item As T) As Boolean Implements ICollection(Of T).Contains
        Return _list.Contains(item)
    End Function

    Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator
        Return InternalEnumerator()
    End Function

    Private Iterator Function InternalEnumerator() As IEnumerator(Of T)
        Dim e As IEnumerator = _list.GetEnumerator()
        While e.MoveNext()
            Dim current As T = CType((CObj(e.Current)), T)
            Yield current
        End While
        Return
    End Function

    Public Function IndexOf(item As T) As Integer Implements IList(Of T).IndexOf
        Return _list.IndexOf(item)
    End Function

    Public Function Remove(item As T) As Boolean Implements ICollection(Of T).Remove
        _list.Remove(item)
        Return True
    End Function

    Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Return _list.GetEnumerator()
    End Function

End Class

Usage:

        Dim list As New RiverNileList(Of String)(New String() {"Omar", "Ahmed", "Ali"})
        For Each s As String In list
            ' do something
        Next

Comments

Popular posts from this blog

مقدمة الي تشفير الحروف الأبجدية العربية

VB.NET Translucent Control using GDI+

Add Custom Event to a Class in VB.NET