4

When creating a dictionary object using early binding and the Microsoft Scripting Runtime Library, it looks like there are 2 equivalent type names for the same thing:

Dim dict as Dictionary
Set dict = New Dictionary

and

Dim dict as Scripting.Dictionary
Set dict = new Scripting.Dictionary

appear to do the same thing (at least so far).

I see the As Scripting.Dictionary syntax used by this resource, and I see the As Dictionary syntax used by this (really great) resource, but I haven't seen a comparison anywhere. And the MSDN documentation on dictionaries is either a literal joke, or not explicitly related to VBA.

I don't see why I should be doing extra typing just to make my function declarations more crowded if I can just use As Dictionary, but I have learned that some things in VBA that look like they are equivalent can actually have subtle but significant differences (Application.InputBox vs. InputBox for example).

Is there is really no difference between these, or are there subtle differences that I should be aware of?

8
  • 1
    You only need to use Scripting.Dictionary for late binding. They are indeed the same thing.
    – braX
    Jan 24, 2018 at 18:22
  • 1
    @braX - Not entirely true. Late binding syntax would be different Dim dict as Object then Set dict = CreateObject("Scripting.Dictionary"). Think it's important to clarify that. Jan 24, 2018 at 18:46
  • 1
    you have to include the MS Scripting run time in your project for dictionary usage. Early and late binding have different behaviors, such as the use of intellisense (early binding) in the VB editor. There is also something about coding with early binding for development and then late binding before deployment to users with different versions of the application, may not be applicable here. excelmacromastery.com/vba-dictionary Jan 24, 2018 at 18:50
  • 1
    FWIW that literal joke is up on GitHub. Feel free to improve it! Jan 24, 2018 at 19:03
  • 1
    IMO, your 'really great resource' is a good place to pick up tips and a bad place to learn how to write code.
    – Rory
    Jan 24, 2018 at 21:26

3 Answers 3

17

I don't see why I should be doing extra typing just to make my function declarations more crowded [...]

That would be what we call fully qualifying a type (or function/procedure call, depending on context). When you do this:

Dim foo As Range

The type Range gets resolved in a very specific order, determined by the priority order, in the references dialog:

Tools/References dialog

If the top-most type library that exposes a Range type is the Excel type library, then Range stands for Excel.Range.

If you meant that to be Word.Range, then you have a bug, and fully-qualifying the declared types prevents this.

The Scripting library exposes a Dictionary class. So when you reference the Scripting type library and do this:

Dim foo As Dictionary

You have all rights to expect that type to resolve to Scripting.Dictionary. But then if you add a class module and name it Dictionary, suddenly all these unqualified declarations are now referring to this custom class, because VBA type resolution will always prioritize types defined in the current project before looking at external references.

There are pros and cons to both approaches:

  • Fully-qualifying types avoids ambiguity when referencing type libraries that expose same-name classes: it enhances readability.
  • Not qualifying types makes it easier to swap a class for another, e.g. replacing Scripting.Dictionary with a portable Dictionary implementation that works on a Mac.

Rule of thumb, programming isn't about writing code, it's... mostly about reading code! Write code that's easy to read, not code that's quick to write - your future self will thank you later!

6
  • 7
    Note that the qualification can lead to a different behavior in some cases. For instance VBA.Array() is always Base 0 while Array() takes the base defined by Option Base which is zero by default.
    – Florent B.
    Jan 24, 2018 at 20:34
  • 3
    @FlorentB. that's correct, but IMO also a bloody evil "feature" - I don't see legitimate reasons to hate VBA (99% of the time it's about the VBIDE), but this is definitely one of them =) Jan 24, 2018 at 20:37
  • Very well explained. I will probably go with As Dictionary because I think it's easier to read function declarations that fit on your screen, and if I ever decide to write a class named Dictionary I will probably want it to supersede Scripting.Dictionary. Your answer inspired me to speed test, and it looks like there's a tiny but consistent speed improvement with Scripting.Dictionary; I assume this is b/c VBA doesn't have to check libraries for a dictionary object. Are there any other (common) libraries w/ a dictionary object that could mess up my code in the future?
    – nateAtwork
    Jan 24, 2018 at 20:43
  • 2
    @nateAtwork that speed test can't possibly be conclusive - the binding occurs at compile time, not run-time; that would be late binding, and late binding should be expected to be somewhat slower, although I doubt it would be observable (hint: anything declared As Object or As Variant will be bound at run-time, that's why you don't get intellisense on anything that returns Object or Variant). As for common libs, not that I know of. ADO and DAO have very similar types, and Word's Range famously clashes with Excel's. Jan 24, 2018 at 20:46
  • 1
    @Mat'sMug I think I follow what you're saying here, but can you fix the link in your comment (it sounds like a great tool...) &/or recommend another resource that explains the general construction of the VBA language?
    – nateAtwork
    Jan 24, 2018 at 22:18
4

As @braX said in your specific use case (early binding) it's the same thing and thus the Scripting. can be left off.

However, there are cases where it may be helpful (or even needed). For example, if you were manipulating Excel in another Office app, you would (using early binding) write

Dim XL as Excel.Application

because you need to tell VBA which application, specifically, to use.

But you could go on to write

Dim myWB as Workbook

and VBA will know it's excel work. However, it may be more user-friendly to write

Dim myWB as Excel.Workbook

That way it's totally clear what is going on.

Now, in the case of a workbook, to most programmers, it's already clear, and that's fine, but what if you are in programming in Word and want to manipulate a Range object in Excel. Writing

Dim myRange as Range

may not work as you think it will because VBA will resolve it as a Word Range Object (since you are writing in Word - see MatMug's answer for better explanation), which will then lead to errors further down the code line when you refer to myRange.Offset(1), for example, because there is no Offset method of Word Range Object. So in this case, it's both necessary and user-friendly.

2
  • 1
    Range won't confuse VBA at all; it might confuse the reader - VBA just resolves Range as it's told, i.e. accordingly with the priority order defined by the tools/references dialog. +1 for everything else :) Jan 24, 2018 at 19:05
  • 1
    Thanks for the clarification @Mat'sMug. I edited to say what I was really trying to say :) Jan 24, 2018 at 19:26
-4

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...

  1. are not compatible in VBA call semantics.
  2. do not have the same external interface.
  3. 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.)

4
  • 2
    On my system none of this is true. For me, Dictionary and Scripting.Dictionary are completely interchangeable Dec 12, 2019 at 0:50
  • 2
    @chrisneilsen tested & seconded. The only way to get a type mismatch is to have a Dictionary user class in the VBAProject. Dec 12, 2019 at 1:44
  • May be he uses Word? Or has a Word reference on top of Scripting.A watch would reveal the type. Dec 12, 2019 at 5:49
  • 1
    @mat or as per your A, a higher priority reference to a type with a Dictionary definition. Maybe that's what's going on here. VTD (as misinformation)? Dec 12, 2019 at 5:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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