As an example, suppose I had a large amount of data about a set of Restaurants for a large set of Dates in a database that I need to analyze / output to the user.

I have a custom class that holds the data for each restaurant for each date:

Public Class DateData
    Public Property Var1 As Double = 0
    Public Property Var2 As Double = 0
    Public Property Var3 As Double = 0
    Public Property Var4 As Double = 0

    Public Sub Sub1()
    End Sub

   ... etc ....
End Class

And since I am getting this data for each date (and since date-order does matter for my calculations), I have the following class set up too (where most of the work / calculation is done):

Public Class RestaurantData
    Inherits SortedDictionary(Of Date, DateData)

   Public Property Name As String
   Public Property RestaurantLevelData1 As Double = 0
   Public Property RestaurantLevelData2 As Double = 0

   Public Sub New(ByVal strName As String, ByVal DatesList As List(Of Date))
       _Name = strName
       For Each daDate As Date In DatesList
   End Sub

   Public Overloads Sub Add(ByVal daDate As Date)
        MyBase.Add(daDate, New DateData)
   End Sub

   Public Sub Sub1()
       For i As Integer = 0 To Me.Keys.Count - 1
           Dim daDate As Date = Me.Keys(i)
           .... etc ....
   End Sub

   ... etc ....
End Class

And, finally, since this is for a large amount of restaurants, I have a class which holds this Dictionary on a restaurant levele:

Public Class RestaurantsDict
    Inherits Dictionary(Of String, RestaurantData)

   Public Overloads Sub Add(ByVal strName As String, ByVal Dates As List(Of Date))
       MyBase.Add(strName, New RestaurantData(strName, Dates))
   End Sub
End Class

I have 2 main questions:

  1. Is this a good way to set this up? My original code consisted of dozens of

    Dictionary(Of String, SortedDictionary(Of Date, Double))

    But I am a fairly novice programmer and would love to know if there is a more efficient architecture for what I am trying to achieve.

  2. The way I am currently setting this up is very hard to debug. For example, the Count property of my RestaurantData throws an exception of type System.TypeLoadException.

  • \$\begingroup\$ Why do you need a SortedDictionary? Other options are: just to sort incoming data by dates and store in array or list; use SortedList \$\endgroup\$ – almaz Dec 12 '12 at 14:38
  • \$\begingroup\$ @almaz, Thanks for the comment and answer. Just to understand how you would suggest doing this, you would simply use a dictionary and have a separate SortedList(Of Date) that I oculd use to hold the keys in a sorted fashion? \$\endgroup\$ – John Bustos Dec 12 '12 at 14:51
  • 2
    \$\begingroup\$ SortedList<TKey, TValue> is pretty much the same as SortedDictionary<TKey, TValue>, the difference is in internal data structures, memory consumption and performance of addition/searching \$\endgroup\$ – almaz Dec 12 '12 at 15:45

Inheriting from collections is rarely a good practice. In your case it's better to have a private collection (SortedDictionary in your case), and populate it as needed (in other words it's better to use delegation than inheritance here).

The reason why inheriting is not so good here is that your class will expose a lot of functionality that users of RestaurantData aren't actually interested in.

|improve this answer|||||
  • \$\begingroup\$ Thanks for the response - Just to understand (since this is all fairly new to me), you are suggesting my initial approach of just having a series of Dictionary(Of...) in my code rather than creating custom objects? My logic for going this way was that it allowed me to create extra Add events and constructors for these specific classes... As a larger-scale question, when do you decide to delegate rather than inherit? Thanks!! \$\endgroup\$ – John Bustos Dec 12 '12 at 14:50
  • 2
    \$\begingroup\$ @JohnBustos: No, you will have one private SortedDictionary inside your RestaurantData class and will potentially have public methods in that class similar to those in SortedDictionary. It is possible that you will want to implement IDictionary or IEnumerable (e.g., so that you can use Linq on RestaurantData). \$\endgroup\$ – Brian Dec 12 '12 at 14:54
  • \$\begingroup\$ Aaaaaah, @Brian... Ok, that's beginning to make sense to me and my thick skull... So I would still have those 3 different classes, just not inherit the Dictionary / SortedDictionary. Another question - the way I have it programmed now allows me to write my code along the lines of MyRestaurantDict("RestName")(#Date#).Var1 - How could I get the same functionality / ease of referencing without inheriting the dictionary classes? Could you (or someone) just point me in the right direction as to how my class definitions could look? Thanks!!! \$\endgroup\$ – John Bustos Dec 12 '12 at 15:37
  • \$\begingroup\$ @JohnBustos You would have to create a new method GetDateData(Date) in RestaurantData that will return required DateData object \$\endgroup\$ – almaz Dec 12 '12 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.