Results 1 to 11 of 11

Thread: collection class with keys?

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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:
    1. 'Copyright (C) 2002 Microsoft Corporation
    2. 'All rights reserved.
    3. 'THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
    4. 'EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
    5. 'MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
    6.  
    7. 'Requires the Trial or Release version of Visual Studio .NET Professional (or greater).
    8.  
    9. Option Strict On
    10.  
    11. Public Class Customers
    12.  
    13.     ' The first thing to do when building a CollectionBase class is
    14.     ' to inherit from System.Collections.CollectionBase
    15.     Inherits System.Collections.CollectionBase
    16.  
    17.     Public Overloads Function Add(ByVal Value As Customer) As Customer
    18.  
    19.         ' After you inherit the CollectionBase class,
    20.         ' you can access an intrinsic object
    21.         ' called InnerList that represents your
    22.         ' collection. InnerList is of type ArrayList.
    23.         Me.InnerList.Add(Value)
    24.         Return Value
    25.  
    26.     End Function
    27.  
    28.     Public Overloads Function Add(ByVal FirstName As String, ByVal LastName As String, ByVal AccountNum As String) As Customer
    29.         Dim cust As New Customer()
    30.  
    31.         cust.FirstName = FirstName
    32.         cust.LastName = LastName
    33.         cust.AccountNumber = AccountNum
    34.  
    35.         ' When you use the InnerList.Add method, you must
    36.         ' pass it an object to add to the collection. In this
    37.         ' case we've created a new Customer object based on
    38.         ' passed parameters, and now we're adding it to the
    39.         ' InnerList.
    40.         Me.InnerList.Add(cust)
    41.  
    42.         Return cust
    43.  
    44.     End Function
    45.  
    46.     Public Overloads Function Item(ByVal Index As Integer) As Customer
    47.  
    48.         ' To retrieve an item from the InnerList, pass
    49.         ' the index of that item to the .Item property.
    50.  
    51.         Return CType(Me.InnerList.Item(Index), Customer)
    52.     End Function
    53.  
    54.     Public Overloads Function Item(ByVal cust As Customer) As Customer
    55.  
    56.         ' To retrieve an item from the InnerList, pass
    57.         ' the index of that item to the .Item property.
    58.  
    59.         Dim myIndex As Integer
    60.  
    61.         myIndex = Me.InnerList.IndexOf(cust)
    62.         Return CType(Me.InnerList.Item(myIndex), Customer)
    63.  
    64.     End Function
    65.  
    66.     Public Overloads Sub Remove(ByVal cust As Customer)
    67.  
    68.         ' To remove an item from the collection, you must
    69.         ' pass in a reference to that item (in this case, the
    70.         ' Customer object we want to remove).
    71.  
    72.         ' However, it is often more convenient to create a
    73.         ' Remove method that allows the calling code to pass in
    74.         ' only the index of the item instead of an object reference.
    75.         ' So we've overloaded the Remove method to use either one.
    76.         Me.InnerList.Remove(cust)
    77.     End Sub
    78.  
    79.     Public Overloads Sub Remove(ByVal Index As Integer)
    80.  
    81.         ' This is the second Remove method. Instead of passing
    82.         ' in an object reference, this Remove expects an index.
    83.  
    84.         ' The calling code can decide which one to call.
    85.  
    86.         ' If the calling code passes an index, you can simply
    87.         ' look up that item by using the InnerList.Item method,
    88.         ' then remove the item.
    89.  
    90.         Dim cust As Customer
    91.  
    92.         cust = CType(Me.InnerList.Item(Index), Customer)
    93.         If Not cust Is Nothing Then
    94.             Me.InnerList.Remove(cust)
    95.         End If
    96.     End Sub
    97.  
    98. End Class

  2. #2

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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?

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    any benefit from using one over the other?

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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)

  6. #6

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    bump

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  8. #8

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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)

  9. #9
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  10. #10
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    If have not tried this but it might be worth a go.

    VB Code:
    1. Private Shadows Sub TheSubYouWantToHide()
    2. 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...

  11. #11

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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
  •  



Click Here to Expand Forum to Full Width