# DICTIONARY OR A 2 ELEMENT ARRAY

I am curious to see what the consensus is regarding the use of the dictionary class. From what I have observed a dictionary is basically a 2 element array. 2 questions;
what would be the advantage(s) of using a dictionary class over creating a 2 element array?
which one uses the most system resources?

Do you mean a two dimensional array? A two element array is something like this:

Dim MyArray(1) as integer

MyArray(0) = 5
MyArray(1) = 6

That’s two elements. A dictionary can have way more than two items in it.

A dictionary can be used in a sense like an array but it’s different. A dictionary is really a lookup table that ends up being very fast and flexible.

Arrays are great for storing an ordered set of data. You could have multi dimensional arrays that store a column of data in each dimension. Etc.

But to find a look up values, arrays are inefficient. Lat’s say you have an array of 10,000 elements and there’s a certain value that you need, but you don’t know where that value is located in the array. Let’s say you have a two dimensional array of customer names and customer IDs. You would do this with arrays:

``````Dim FindValue as String = "John Smith"
Dim CustomerNumber as Integer

For i =  0 to Ubound(MyStringArray,1)
If MyStringArray(i,0) = "JohnSmith" Then
CustomerNumber = MyStringArray(i,1).Val
Exit
End If
Next
``````

If John Smith is located at element 9050, the you have a lot of loops with wasted effort going on. If you use a dictionary and want to return John Smith’s customer ID number, then you can do something like this:

``````
//First Add the ID to the dictionary
MyDictionary.Value("John Smith") = 2503286946039
``````

Now later, you want to find that ID later, we simply do:

``````Dim CustomerNumber as integer

CustomerNumber = MyDictionary.Value("John Smith").IntegerValue
End If
``````

No looping through 9049 items to find the right one. Boom! It gets it.

And the magical thing about dictionaries is that since they hold Variants, you can store just about anything in them. Objects, strings, etc. They are REALLY powerful. Once you get your head around them they are great.

Don’t use an object as a key though for the dictionary. That will screw up your lookups. Strings and integers work really well as the key values.

Hope this helps.

[quote=126615:@chris benton]I am curious to see what the consensus is regarding the use of the dictionary class. From what I have observed a dictionary is basically a 2 element array. 2 questions;
what would be the advantage(s) of using a dictionary class over creating a 2 element array?
which one uses the most system resources?[/quote]
Well  not really a 2 element array
It’s a key value store
So you can use any KEY (a class instance, a string, integer, etc) and any value (same list)
To use an array this way would be much slower

And because the dictionary uses a hashing mechanism to figure out what keys it holds it can be very quick to determine if it has contents.

Contrary to what Jon suggested objects as keys works just fine - the IDE uses that in spots

[quote=126675:@Norman Palardy]
Contrary to what Jon suggested objects as keys works just fine - the IDE uses that in spots[/quote]

I only say that because I’ve seen issues where you can have two objects that appear to be the same object but really are not the same object. So if you attempt to look up a key where the object being used as they key is not the object you are using for the lookup, you’ll get a key not found exception.

So it certainly can be done, it’s just ticker. I forget the exact example I had from a while back. But someone suggested not using objects as keys.

I remember testing speed and it wasn’t faster than using an class array (around 2006). The nice thing about a class array is you can hold any amount of information you need.

At doing what? There are certainly uses for both, but if you’re suggesting that an array is just as quick as a dictionary at finding a key-value pair, I’d sure that to see the code which demonstrates that.

If you have show object id’s on for debugging then thats the only thing to look at with objects
Not content - just their ID’s
Same id = same object
Not the same ID not the same object - regardless of contents

Same with dictionary
In fact you can fake an array with a dictionary - not sure you’d be able to do the reverse
Just use integers for the keys