John,
The following was originally posted by me 23 August 2003 under the
"ArrayList ToArray issues" thread in this newsgroup.
Attached are extended CollectionBase & DictonaryBase classes, that should do
what you want.
I wrote them to mimic CollectionBase & DictionaryBase as close as possible.
The differences being is they have a constructor that you can pass the inner
container to the constructor. And the InnerList changed to an IList type,
while InnerHashtable changed to InnerDictionary of type IDictionary.
CollectionBaseEx accepts an IList in the constructor, if you do not give
one, ArrayList is used. For example you could pass an array of objects,
making a fixed size list, instead of an ArrayList. InnerList does not do
validation, List does validation.
DictionaryBaseEx accepts an IDictionary in the constructor, if you do not
give one, HashTable is used. For example you can pass SortedList,
HybridDictionary, ListDictionary, instead of a HashTable. InnerDictionary
does not do validation, Dictionary does validation.
You can derive from DictionaryBaseEx then pass a new SortedList to the
constructor.
Something like:
Public Class FrameSortedList
Inherits ExDictionaryBase
Public Sub New()
MyBase.New(New SortedList)
End Sub
' other methods you want
End Class
FWIW: I actually started with the m_dictionary field in DictionaryBaseEx as
type SortedList, but then noticed I did not rely on it specifically being a
SortedList, so I modified DictionaryBaseEx to support any IDictionary,
which IMHO actually makes the class far more flexible.
If you use them, let me know if you find any problems with them.
Hope this helps
Jay
---x--- Begin DictionaryBaseEx.vb ---x---
<Serializable()> _
Public MustInherit Class DictionaryBaseEx
Implements IDictionary
Private ReadOnly m_dictionary As IDictionary
Protected Sub New()
MyClass.New(New Hashtable)
End Sub
Protected Sub New(ByVal dictionary As IDictionary)
If dictionary Is Nothing Then
Throw New ArgumentNullException("dictionary")
End If
m_dictionary = dictionary
End Sub
Protected ReadOnly Property InnerDictionary() As IDictionary
Get
Return m_dictionary
End Get
End Property
Protected ReadOnly Property Dictionary() As IDictionary
Get
Return Me
End Get
End Property
Public ReadOnly Property Count() As Integer Implements ICollection.Count
Get
Return m_dictionary.Count
End Get
End Property
Public Sub Clear() Implements IDictionary.Clear
Me.OnClear()
m_dictionary.Clear()
Me.OnClearComplete()
End Sub
Public Sub CopyTo(ByVal array As System.Array, ByVal index As Integer)
Implements ICollection.CopyTo
m_dictionary.CopyTo(array, index)
End Sub
Public Function GetEnumerator() As IDictionaryEnumerator Implements
IDictionary.GetEnumerator
Return m_dictionary.GetEnumerator
End Function
#Region " IDictionary support "
Private ReadOnly Property IDictionary_IsSynchronized() As Boolean
Implements ICollection.IsSynchronized
Get
Return m_dictionary.IsSynchronized
End Get
End Property
Private ReadOnly Property IDictionary_SyncRoot() As Object Implements
ICollection.SyncRoot
Get
Return m_dictionary.SyncRoot
End Get
End Property
Private Function IEnumerable_GetEnumerator() As IEnumerator Implements
IEnumerable.GetEnumerator
Return m_dictionary.GetEnumerator()
End Function
Private Sub IDictionary_Add(ByVal key As Object, ByVal value As Object)
Implements IDictionary.Add
Me.OnValidate(key, value)
Me.OnInsert(key, value)
m_dictionary.Add(key, value)
Me.OnInsertComplete(key, value)
End Sub
Private Function IDictionary_Contains(ByVal key As Object) As Boolean
Implements IDictionary.Contains
Return m_dictionary.Contains(key)
End Function
Private ReadOnly Property IDictionary_IsFixedSize() As Boolean
Implements IDictionary.IsFixedSize
Get
Return m_dictionary.IsFixedSize
End Get
End Property
Private ReadOnly Property IDictionary_IsReadOnly() As Boolean Implements
IDictionary.IsReadOnly
Get
Return m_dictionary.IsReadOnly
End Get
End Property
Private Property IDictionary_Item(ByVal key As Object) As Object
Implements IDictionary.Item
Get
Dim currentValue As Object = m_dictionary.Item(key)
currentValue = Me.OnGet(key, currentValue)
Return currentValue
End Get
Set(ByVal value As Object)
Dim oldValue As Object = m_dictionary.Item(key)
Me.OnValidate(key, value)
Me.OnSet(key, oldValue, value)
m_dictionary.Item(key) = value
Me.OnSetComplete(key, oldValue, value)
End Set
End Property
Private ReadOnly Property IDictionary_Keys() As ICollection Implements
IDictionary.Keys
Get
Return m_dictionary.Keys
End Get
End Property
Private Sub IDictionary_Remove(ByVal key As Object) Implements
IDictionary.Remove
Dim value As Object = m_dictionary.Item(key)
Me.OnValidate(key, value)
Me.OnRemove(key, value)
m_dictionary.Remove(key)
Me.OnRemoveComplete(key, value)
End Sub
Private ReadOnly Property IDictionary_Values() As ICollection Implements
IDictionary.Values
Get
Return m_dictionary.Values
End Get
End Property
#End Region
#Region " Custom processing support "
Protected Overridable Sub OnClear()
End Sub
Protected Overridable Sub OnClearComplete()
End Sub
Protected Overridable Function OnGet(ByVal key As Object, ByVal
currentValue As Object) As Object
Return currentValue
End Function
Protected Overridable Sub OnInsert(ByVal key As Object, ByVal value As
Object)
End Sub
Protected Overridable Sub OnInsertComplete(ByVal key As Object, ByVal
value As Object)
End Sub
Protected Overridable Sub OnRemove(ByVal key As Object, ByVal value As
Object)
End Sub
Protected Overridable Sub OnRemoveComplete(ByVal key As Object, ByVal
value As Object)
End Sub
Protected Overridable Sub OnSet(ByVal key As Object, ByVal oldValue As
Object, ByVal newValue As Object)
End Sub
Protected Overridable Sub OnSetComplete(ByVal key As Object, ByVal
oldValue As Object, ByVal newValue As Object)
End Sub
Protected Overridable Sub OnValidate(ByVal key As Object, ByVal value As
Object)
End Sub
#End Region
End Class
---x--- End DictionaryBaseEx.vb ---x---
---x--- Begin CollectionBaseEx.vb ---x---
<Serializable()> _
Public MustInherit Class CollectionBaseEx
Implements IList
Private ReadOnly m_list As IList
Protected Sub New()
MyClass.New(New ArrayList)
End Sub
Protected Sub New(ByVal list As IList)
If list Is Nothing Then
Throw New ArgumentNullException("list")
End If
m_list = list
End Sub
Protected ReadOnly Property InnerList() As IList
Get
Return m_list
End Get
End Property
Protected ReadOnly Property List() As IList
Get
Return Me
End Get
End Property
Public ReadOnly Property Count() As Integer Implements ICollection.Count
Get
Return m_list.Count
End Get
End Property
Public Sub Clear() Implements IList.Clear
Me.OnClear()
m_list.Clear()
Me.OnClearComplete()
End Sub
Public Function GetEnumerator() As IEnumerator Implements
IList.GetEnumerator
Return m_list.GetEnumerator
End Function
Public Sub RemoveAt(ByVal index As Integer) Implements IList.RemoveAt
Dim value As Object = m_list(index)
Me.OnValidate(value)
Me.OnRemove(index, value)
m_list.RemoveAt(index)
Me.OnRemoveComplete(index, value)
End Sub
#Region " IList support "
Private ReadOnly Property IList_IsSynchronized() As Boolean Implements
ICollection.IsSynchronized
Get
Return m_list.IsSynchronized
End Get
End Property
Private ReadOnly Property IList_SyncRoot() As Object Implements
ICollection.SyncRoot
Get
Return m_list.SyncRoot
End Get
End Property
Private ReadOnly Property IList_IsFixedSize() As Boolean Implements
IList.IsFixedSize
Get
Return m_list.IsFixedSize
End Get
End Property
Private ReadOnly Property IList_IsReadOnly() As Boolean Implements
IList.IsReadOnly
Get
Return m_list.IsReadOnly
End Get
End Property
Private Function IList_Add(ByVal value As Object) As Integer Implements
IList.Add
Dim index As Integer = m_list.Count
Me.OnValidate(value)
Me.OnInsert(index, value)
Return m_list.Add(value)
Me.OnInsertComplete(index, value)
End Function
Private Sub IList_CopyTo(ByVal array As System.Array, ByVal index As
Integer) Implements ICollection.CopyTo
m_list.CopyTo(array, index)
End Sub
Private Function IList_Contains(ByVal value As Object) As Boolean
Implements IList.Contains
Return m_list.Contains(value)
End Function
Private Function IList_IndexOf(ByVal value As Object) As Integer
Implements IList.IndexOf
Return m_list.IndexOf(value)
End Function
Private Sub IList_Insert(ByVal index As Integer, ByVal value As Object)
Implements IList.Insert
Me.OnValidate(value)
Me.OnInsert(index, value)
m_list.Insert(index, value)
Me.OnInsertComplete(index, value)
End Sub
Private Property IList_Item(ByVal index As Integer) As Object Implements
IList.Item
Get
Dim currentValue As Object = m_list.Item(index)
currentValue = Me.OnGet(index, currentValue)
Return currentValue
End Get
Set(ByVal value As Object)
Dim oldValue As Object = m_list(index)
Me.OnValidate(value)
Me.OnSet(index, oldValue, value)
m_list(index) = value
Me.OnSetComplete(index, oldValue, value)
End Set
End Property
Private Sub IList_Remove(ByVal value As Object) Implements IList.Remove
Dim index As Integer = m_list.IndexOf(value)
Me.OnValidate(value)
Me.OnRemove(index, value)
m_list.Remove(value)
Me.OnRemoveComplete(index, value)
End Sub
#End Region
#Region " Custom processing support "
Protected Overridable Sub OnClear()
End Sub
Protected Overridable Sub OnClearComplete()
End Sub
Protected Overridable Function OnGet(ByVal index As Integer, ByVal
currentValue As Object) As Object
Return currentValue
End Function
Protected Overridable Sub OnInsert(ByVal index As Integer, ByVal value
As Object)
End Sub
Protected Overridable Sub OnInsertComplete(ByVal index As Integer, ByVal
value As Object)
End Sub
Protected Overridable Sub OnRemove(ByVal index As Integer, ByVal value
As Object)
End Sub
Protected Overridable Sub OnRemoveComplete(ByVal index As Integer, ByVal
value As Object)
End Sub
Protected Overridable Sub OnSet(ByVal index As Integer, ByVal oldValue
As Object, ByVal newValue As Object)
End Sub
Protected Overridable Sub OnSetComplete(ByVal index As Integer, ByVal
oldValue As Object, ByVal newValue As Object)
End Sub
Protected Overridable Sub OnValidate(ByVal value As Object)
End Sub
#End Region
End Class
---x--- End CollectionBaseEx.vb ---x---
"J L" <jo**@marymonte.com> wrote in message
news:f4********************************@4ax.com...
Hi Jay,
Can you create a strongly typed SortedList also? If so how?
TIA,
John
On Fri, 25 Mar 2005 14:02:15 -0600, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
Dennis,
In addition to the other comments, I normally inherit from CollectionBase
when I need a strongly typed arraylist. I normally inherit from
DictionaryBase when I need a strongly typed HashTable.
Something like:
Friend Class FrameList
Inherits CollectionBase
Public Sub Add(ByVal value As FrameStructure)
MyBase.InnerList.Add(value)
End Sub
Default Public Property Item(ByVal Index As Integer) As FrameStructure
Get
Return DirectCast(MyBase.InnerList(Index), FrameStructure)
End Get
Set(ByVal Value As FrameStructure)
MyBase.InnerList(Index) = Value
End Set
End Property
End Class
This allows adding strongly typed methods, such as Add, to the FrameList
also.
Hope this helps
Jay
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:22**********************************@micros oft.com...I use the following code for a strongly typed arraylist and it works
great.
However, I was wondering if this is the proper way to do it. I realize
that
if I want to implement sorting of the arraylist then I have to handle
this
with a sort method that uses comparer. I can reference the properties
of
the
Arraylist directly such as
dim mylist as new FrameList
mylist.Add(new FrameStructure)
mylist(0).first = "blabla..."
mylist(0).second = "bla2 bla2..."
'Strongly typed arraylist class "FrameList"
Friend Class FrameList
Inherits ArrayList
Default Public Shadows Property Item(ByVal Index As Integer) As
FrameStructure
Get
Return DirectCast(MyBase.Item(Index), FrameStructure)
End Get
Set(ByVal Value As FrameStructure)
MyBase.Item(Index) = Value
End Set
End Property
End Class
public class FrameStructure
public first as string
public second as string
end class
--
Dennis in Houston