# Visual Basic > Visual Basic FAQs >  VB.NET[arrays] How to make/use Associative Arrays

## Kaiser Sousse

Arrays can be quite useful for a variety of tasks, but occasionally there will be times when you can't reference them numerically or with pre-defined constant values.

For instance, say you have an app that references a database to display a catalog of items to buy (the "display" element of a shopping cart).  The items are taken from multiple tables which eliminates the chance of using simple numeric arrays because there may be conflicting primary keys to use as the index when the items are gathered together.  They do however all have unique product codes (alhpa-numeric values).

Since you wouldn't want to overload the database by tracking on-the-fly cart status for each order as it was built for each customer, it would be better to build and update the "tally" element of the shopping cart in memory.

You can do this each time the "tally" needs to be displayed or updated (say if quantities are changed or items deleted) by storing the product code for each item that the customer has selected as the key and the quantity for that item as the value.  If the customer subsequently adds an item that is already in the cart, the quantity is augmented, if an item doesn't currently exist, a new element is created, if a quantity is changed to zero, that element essentially goes away (the element would technically still be there, with a value of zero, but if your code only displayed and totalled items with quantities of one or more, it would appear deleted).

In VB.NET, there is no associative array (per se), but there are dictionaries that perform in a near-identical manner... some refer to this as a hash-table, although a dictionary (being a derivative of a hash-table) has less memory overhead (hash-tables are always objects... dictionaries can be defined as most anything)

here are a few sample code snippets to show the syntax (these are not meant to work by themselves... alot of additional code would be necessary to do anything useful): 

*
'create a dictionary object (associative array)

Dim StoreItem As New Dictionary(Of String , Integer )


'create quantities (this would happen as the user added to the cart)

StoreItem.Add("CS2a", 6)
StoreItem.Add("DD72b12", 3)
StoreItem.Add("21AG4", 1)
etc...

'reading is just as easy (although you probably wouldn't use a msgbox   )
MsgBox(StoreItem("DD72b12"))
*

NOTE:  By using a combination of multi-dimensioned arrays/dictionaries and looping structures, there is a virtually limitless amount as to what you may use this for...  hierarchies of states, regions, cities, zips; or perhaps defining structures of departments and/or personnel; schedules available vs. filled, etc, etc...

I recently had a client ask me to build a fairly complex scheduling app that allowed them to define events, the dates they would fall on, times (shifts) that required staffing, and the maximum number of volunteers needed for each available shift.  I had to build a calendar-like scheduler and provided a multitude of reporting and tracking capabilities.  After a fairly extensive analysis of the problem, I wound up crafting a solution using nothing but associative arrays and it wound up being quite fast and eliminated a considerable amount of technical issues I would otherwise have had to deal with.  Since deployment, maintenance has been a breeze as the logic is painfully simple.

.

----------

