Advice needed on Dictionary or Array?

I have an issue where I need to hold product information with a lookup using a SKU code so I was thinking about using a dictionary or an array and then thought maybe a class would be better but now I am feeling totally confused. I came up with 4 options but not sure which one would be fastest to search through etc. Any better suggestions welcome. What I need to be able to say is give me product information for SKU code XXXXX

Option 1 - Two dimensional array with column 0 holding the SKU code and column 1 holding a dictionary of product information
Option 2 - Dictionary with key as SKU code and value as another dictionary holding product information
Option 3 - Two dimentional array with column 0 holding the SKU code and column 1 holding a class with properties for the product information
Option 4 - Dictionary with key as SKU code and value as a class with properties for the product information

I would totally reject #1 and #2 and lean towards #4
and even more so with #5

#5 - use a small SQLite Relational database, indexed by SKU

I would recommend your #4. It think it is fast, simple and easy to understand. A class allows you to clearly define what can be stored. Using arrays will be slow to look up SKU codes since you’ll have to loop through the array each time to find one. Using a Dictionary containing Dictionaries does not clearly define what can be in the “subdictionaries”. Bugs will result.

If you are dealing with a large amount of data and memory is a concern, then using a SQLite DB would be the next logical step.

Personally - I would go with Dave’s #5

At the moment you may be able to hold what you need in a dictionary setup but if that need grows then you may have to re-engineer. If you go with a SQLite DB then your just working with an existing relational DB.

Thanks everyone, that is really helpful. I already hold all the product data in a MS SQL database but I dont want to keep going back to the server every time to get info about products as the user moves around the app so want to hold it in memory. I think I will go with option 4 and put it in a method so if I need to tweak things later then it will be easier.

Here is what I’d do… Implement a function that you call to get the Product class. In that function, create a static dictionary to cache values. If the cache does not contain the SKU you are looking for, look it up in the db and then store it in the cache. This will do a few things for you, including the most important, allow you to change the implementation in the future as needs change. Further, it will allow you to benchmark. It may be that going to the db isn’t as big of a deal as you think, especially if it is indexed properly.

Here is an example method:

Function GetProduct(sku As String) As Product
    Static cache As Dictionary = New Dictionary

    Dim p As Product = cache.Lookup(sku, nil)
    If p = Nil Then
        Static ps As PreparedStatement = Db.Prepare("SELECT * FROM product WHERE sku=?")
        Dim rs As RecordSet = ps.SQLSelect(sku)
        If Not rs.EOF Then
            p = New Product
            p.Name = rs.Field("name")
            ...etc...
        End If
    End If

    Return p
End Function

Now, to benchmark, just look up how many products your customer may deal with in a given day, say 300. Then divide the time taken across how many hours the person would be working to do that number of lookups and see how much time it actually took.

Then comment out the “= cache.Lookup(…)” line and do the same process. Look at the time difference.

Anyway, this sort of abstraction in general is good. You’ll find as you hide the implementation from the use, your program becomes easier to maintain/refactor.

Wow, thanks for the detailed reply. I am going to start playing with this today.