|
-
Jul 19th, 2004, 01:57 PM
#1
collection class with keys?
I have found a few examples of creating your own collection classes (although they all appear to be ripped off from the original MS one on msdn)
but non of them specify information regarding keys. Does any have an example of a custom collection class in .net using keys instead of indexes? or alongside indexes?
VB Code:
'Copyright (C) 2002 Microsoft Corporation
'All rights reserved.
'THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
'EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
'MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'Requires the Trial or Release version of Visual Studio .NET Professional (or greater).
Option Strict On
Public Class Customers
' The first thing to do when building a CollectionBase class is
' to inherit from System.Collections.CollectionBase
Inherits System.Collections.CollectionBase
Public Overloads Function Add(ByVal Value As Customer) As Customer
' After you inherit the CollectionBase class,
' you can access an intrinsic object
' called InnerList that represents your
' collection. InnerList is of type ArrayList.
Me.InnerList.Add(Value)
Return Value
End Function
Public Overloads Function Add(ByVal FirstName As String, ByVal LastName As String, ByVal AccountNum As String) As Customer
Dim cust As New Customer()
cust.FirstName = FirstName
cust.LastName = LastName
cust.AccountNumber = AccountNum
' When you use the InnerList.Add method, you must
' pass it an object to add to the collection. In this
' case we've created a new Customer object based on
' passed parameters, and now we're adding it to the
' InnerList.
Me.InnerList.Add(cust)
Return cust
End Function
Public Overloads Function Item(ByVal Index As Integer) As Customer
' To retrieve an item from the InnerList, pass
' the index of that item to the .Item property.
Return CType(Me.InnerList.Item(Index), Customer)
End Function
Public Overloads Function Item(ByVal cust As Customer) As Customer
' To retrieve an item from the InnerList, pass
' the index of that item to the .Item property.
Dim myIndex As Integer
myIndex = Me.InnerList.IndexOf(cust)
Return CType(Me.InnerList.Item(myIndex), Customer)
End Function
Public Overloads Sub Remove(ByVal cust As Customer)
' To remove an item from the collection, you must
' pass in a reference to that item (in this case, the
' Customer object we want to remove).
' However, it is often more convenient to create a
' Remove method that allows the calling code to pass in
' only the index of the item instead of an object reference.
' So we've overloaded the Remove method to use either one.
Me.InnerList.Remove(cust)
End Sub
Public Overloads Sub Remove(ByVal Index As Integer)
' This is the second Remove method. Instead of passing
' in an object reference, this Remove expects an index.
' The calling code can decide which one to call.
' If the calling code passes an index, you can simply
' look up that item by using the InnerList.Item method,
' then remove the item.
Dim cust As Customer
cust = CType(Me.InnerList.Item(Index), Customer)
If Not cust Is Nothing Then
Me.InnerList.Remove(cust)
End If
End Sub
End Class
-
Jul 19th, 2004, 02:00 PM
#2
I noticed in a book I have an interface called IDictionary. I guess it is like the scriping.dictionary (which was like the vb6 collection class)
is this what I should be using instead?
-
Jul 19th, 2004, 02:03 PM
#3
I would inherit from Hashtable or DictionaryBase
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jul 19th, 2004, 02:07 PM
#4
any benefit from using one over the other?
-
Jul 19th, 2004, 02:51 PM
#5
maybe I am just missing a key feature or something, but whats the difference between making a class that inherits from
System.Collections.Hashtable
where I have to overload subs like Add, Remove, etc with my own subs
or just making my class and dimming a private variable as a Hashtable and creating subs to interface with the private variable?
Is the ONLY difference that I get all the built in functions of the hashtable without a need to wrap them in my own exposed methods/properties?
(when I say ONLY, it is a very powerful functionality, I am not denying it that, just want to make sure I understand it correctly)
-
Jul 20th, 2004, 08:18 AM
#6
-
Jul 20th, 2004, 09:21 AM
#7
Not only do you get the exposed functionality by inheriting, but you also get access to any protected functionality that the class may offer. That's functions/data available only to the class and its subclasses, in case you didn't know.
Another thing is that you lose the ability to cast into the superclass when you don't inherit from it. In other words, you can't say MyCollectionClass is a Hashtable, because it isn't. But if you inherit from Hashtable, you can.
Nothing really earth-shattering, just some things to consider.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jul 20th, 2004, 11:45 AM
#8
when inheriting, is there any way to NOT allow certain functionality? Is the only way to override the routine and then basically do nothing in your routine or give a msgbox saying functionality is disabled? like say the add routine in a hashtable (I know its not realistic, but just an example)
-
Jul 20th, 2004, 12:13 PM
#9
I don't think you can hide functionality, no. At least not when inheriting.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jul 20th, 2004, 03:29 PM
#10
If have not tried this but it might be worth a go.
VB Code:
Private Shadows Sub TheSubYouWantToHide()
End Sub
In theory that should replace the original method, but be hidden because it's now private.
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Jul 21st, 2004, 02:59 PM
#11
Protected Property Dictionary: Gets the list of elements contained in the DictionaryBase instance.
Protected Property InnerHashtable: Gets the list of elements contained in the DictionaryBase instance.
this is from the help file. Does that mean they both point to the same thing in the base class?
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
|