Mega Code Archive

 
Categories / VB.Net Tutorial / Collections
 

Inherit CollectionBase to create your own Collection structure

Option Strict On Imports System.Collections Public Class SortedStrings    Inherits CollectionBase   Public Function Add(ByVal value As String) As Integer      MyBase.List.Add(value)            Sort()   End Function   Default Public Property Item(ByVal index As Integer) As String      Get         Return DirectCast(MyBase.List.Item(index), String)      End Get      Set(ByVal value As String)         MyBase.List.Item(index) = value      End Set   End Property   Public Sub Sort      Dim sArr(Me.Count - 1) As String      Dim IColl As ICollection = Me      IColl.CopyTo(sArr,0)      Array.Sort(sArr)      Me.Clear      For Each s As String In sArr         Me.List.Add(s)      Next   End Sub End Class Public Module TestSortedStrings    Public Sub Main()       Dim sstrings As New SortedStrings()       sstrings.Add("D")       sstrings.Add("A")       sstrings.Add("C")       sstrings.Add("B")       sstrings.Add("E")              For Each sstring As String In sstrings          Console.WriteLine(sstring)       Next    End Sub End Module A B C D E