All these answers say right things but are ultimately wrong. Let's find out why. Here are some reasons why you might say that Scripting.Dictionary and Dictionary are the same:
- Both are provided by the Microsoft Scripting Runtime reference.
- Both are managed dictionaries and have similar interfaces
Why might we say they're different? Consider:
Sub frontEnd()
Dim dictA As Scripting.Dictionary
Dim dictB As Dictionary
Set dictA = backEnd()
Set dictB = backEnd()
End Sub
Function backEnd() As Scripting.Dictionary
Dim myDict As Scripting.Dictionary
Set myDict = New Scripting.Dictionary
myDict("foo") = "bar"
myDict("bar") = "foo"
Set backEnd = myDict
End Function
Run this code and you'll find that VBA gives you a "type mismatch" error. So, apparently, one cannot assign a Scripting.Dictionary to a variable of type Dictionary. That alone is reason for me to regard these two types as different. Note this is transparent for those of us who use Variant for everything under the sun, but for those of us who program with Option Explicit, this is a strong reason why we should regard these two types as different.
Furthermore, the interfaces may be similar, but they are definitely not the same! Consider:
Sub frontEnd()
Dim dictA As Scripting.Dictionary
Dim dictB As Dictionary
Set dictA = backEnd()
Set dictB = altBackEnd()
End Sub
Function backEnd() As Scripting.Dictionary
Dim myDict As Scripting.Dictionary
Set myDict = New Scripting.Dictionary
myDict("foo") = "bar"
myDict("bar") = "foo"
Set backEnd = myDict
End Function
Function altBackEnd() As Dictionary
Dim myDict As Dictionary
Set myDict = New Dictionary
myDict("foo") = "bar"
myDict("bar") = "foo"
Set altBackEnd = myDict
End Function
Oh, oh! There's a "Key not found" error in the altBackEnd() function. Apparently, a Scripting.Dictionary will allow you to create a Key/Item pair implicitly using the dict("foo") = "bar" semantic, but a Dictionary requires you to use the dict.add() method. Like this:
Sub frontEnd()
Dim dictA As Scripting.Dictionary
Dim dictB As Dictionary
Set dictA = backEnd()
Set dictB = altBackEnd()
End Sub
Function backEnd() As Scripting.Dictionary
Dim myDict As Scripting.Dictionary
Set myDict = New Scripting.Dictionary
myDict("foo") = "bar"
myDict("bar") = "foo"
Set backEnd = myDict
End Function
Function altBackEnd() As Dictionary
Dim myDict As Dictionary
Set myDict = New Dictionary
myDict.Add Key:="foo", Value:="bar"
myDict.Add Key:="bar", Value:="foo"
Set altBackEnd = myDict
End Function
As a last demonstration why these types are different, try replace backEnd() with this version and see what happens:
Function backEnd() As Scripting.Dictionary
Dim myDict As Scripting.Dictionary
Set myDict = New Scripting.Dictionary
myDict.Add Key:="foo", Value:="bar"
myDict.Add Key:="bar", Value:="foo"
Set backEnd = myDict
End Function
Hint: apparently a Dictionary uses a Key/Value pair, but a Scripting.Dictionary uses a Key/Item pair.
As a final homework assignment, get the program to work, post a breakpoint, and use the VBE to look at the structure of both your Scripting.Dictionary and your Dictionary. Notice that they are implemented differently!
Conclusions: Dictionary and Scripting.Dictionary...
- are not compatible in VBA call semantics.
- do not have the same external interface.
- do not have the same internal interface.
Ergo, despite the fact that both are provided by the Microsoft Scripting Runtime reference, they are different.
You're better off using Scripting.Dictionary because it has a much richer interface than Dictionary (e.g. a Scripting.Dictionary has a Remove() and RemoveAll() method, while the Dictionary does not.)
Scripting.Dictionary
for late binding. They are indeed the same thing.Dim dict as Object
thenSet dict = CreateObject("Scripting.Dictionary")
. Think it's important to clarify that.