Mega Code Archive

 
Categories / VB.Net / Data Structure
 

NameObjectCollectionBase BaseGetAllKeys Method returns a String array that contains all the keys

Imports System Imports System.Collections Imports System.Collections.Specialized Public Class MyCollection    Inherits NameObjectCollectionBase    Private _de As New DictionaryEntry()    Default Public ReadOnly Property Item(index As Integer) As DictionaryEntry       Get          _de.Key = Me.BaseGetKey(index)          _de.Value = Me.BaseGet(index)          Return _de       End Get    End Property    Public Sub New(d As IDictionary)       Dim de As DictionaryEntry       For Each de In  d          Me.BaseAdd(CType(de.Key, String), de.Value)       Next de    End Sub 'New    Public ReadOnly Property AllKeys() As String()       Get          Return Me.BaseGetAllKeys()       End Get    End Property    Public ReadOnly Property AllValues() As Array       Get          Return Me.BaseGetAllValues()       End Get    End Property    Public ReadOnly Property AllStringValues() As String()       Get          Return CType(Me.BaseGetAllValues(GetType(String)), String())       End Get    End Property End Class  Public Class SamplesNameObjectCollectionBase    Public Shared Sub Main()       Dim d As New ListDictionary()       d.Add("A", "a")       d.Add("B", "b")       d.Add("P", "p")       Dim myCol As New MyCollection(d)       Console.WriteLine("Initial state of the collection (Count = {0}):", myCol.Count)       PrintKeysAndValues(myCol)       Console.WriteLine("keys:")       Dim s As String       For Each s In  myCol.AllKeys          Console.WriteLine("   {0}", s)       Next s       Console.WriteLine("values (Object):")       Dim o As Object       For Each o In  myCol.AllValues          Console.WriteLine("   {0}", o.ToString())       Next o       Console.WriteLine("values (String):")       For Each s In  myCol.AllValues          Console.WriteLine("   {0}", s)       Next s    End Sub     Public Shared Sub PrintKeysAndValues(myCol As MyCollection)       Dim i As Integer       For i = 0 To myCol.Count - 1          Console.WriteLine("[{0}] : {1}, {2}", i, myCol(i).Key, myCol(i).Value)       Next i    End Sub  End Class