|
-
Apr 25th, 2004, 01:30 AM
#1
Thread Starter
Member
Creating My Own Collection Class
Hi
I need example code on how to write my own Collection class with an Add method that allows me to specify a key. The following is a cut 'n paste from MSDN:
' Restricts to Widget types, items that can be added to the collection.
Public Sub Add(ByVal awidget As Widget)
' Invokes Add method of the List object to add a widget.
List.Add(aWidget)
End Sub
Now, I would like to expand this method to include another argument that represents the key so that when I add objects to my collection, I can use a statement like this:
myCollection.Add(aWidget, key)
Your help will be greatly appreciated
Thanks
-
Apr 25th, 2004, 08:17 PM
#2
Addicted Member
Something like this should do the trick.
VB Code:
Public Class MyCollection
Inherits HashTable
Default Shadows Public ReadOnly Property Item(ByVal Key As Object) As Widget
Get
Return CType(MyBase.Item(Key), Widget)
End Get
End Property
Public Shadows Sub Add(ByVal aWidget As Widget, ByVal Key As Object)
MyBase.Add(Key, aWidget)
End Sub
-
Apr 26th, 2004, 04:32 AM
#3
Thread Starter
Member
Thank you so much.
I'm sure it's going to solve most of my problems. The problem with designing the collection class that inherits from System.Collections.CollectionBase was that I couldn't specify a key with the Add method.
So I reckon the remove method would be the same, thus:
VB Code:
Public Shadows Sub Remove(ByVal Key As Object)
MyBase.Remove(Key)
End Sub
Not sure if it's necessary to implement it as it 's the same as the inherited method.
-
Apr 26th, 2004, 10:00 AM
#4
Addicted Member
Exactly, it is the same as the base class. There is no need to shadow it at all, as you are working with the same data types so the inherited functionality will work fine.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|