Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Also posted on Stack Overflow

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.

So, in my code, I have a custom class that holds the data for each restaurant for each date - For example:

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
           Me.Add(daDate)
       Next
   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)
           Me(daDate).Sub1()
           .... etc ....
       Next
   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 level - For example:

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

Ok, so now that I've set this up, 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.

Overall, this solution is working perfectly well, but I would like to learn from this about how to become a better .Net & OOP programmer.

share|improve this question
    
Why do you need a SortedDictionary? Other options are: just to sort incoming data by dates and store in array or list; use SortedList –  almaz Dec 12 '12 at 14:38
    
@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? –  John Bustos Dec 12 '12 at 14:51
2  
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 –  almaz Dec 12 '12 at 15:45
add comment

1 Answer

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.

share|improve this answer
    
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!! –  John Bustos Dec 12 '12 at 14:50
1  
@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). –  Brian Dec 12 '12 at 14:54
    
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!!! –  John Bustos Dec 12 '12 at 15:37
    
@JohnBustos You would have to create a new method GetDateData(Date) in RestaurantData that will return required DateData object –  almaz Dec 12 '12 at 15:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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