Dictionary and objects

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Dictionary and objects

Post by agibsonsw »

Hello.

Can the Microsoft Scripting Runtime, Dictionary object store and manage objects? That is, can I store Object, Key value pairs?

Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 79676
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Dictionary and objects

Post by HansV »

Yes, that works. Here is an extremely simple example (in Excel):

Code: Select all

Dim rng1 As Range
Dim rng2 As Range
Dim dic As New Scripting.Dictionary

' Set object variable
Set rng1 = Range("A1")
' Add object to dictionary
dic.Add "MyCell", rng1

' Retrieve object from dictionary
Set rng2 = dic("MyCell")
' Do something with it
Debug.Print rng2.Address, rng2.Value
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Dictionary and objects

Post by agibsonsw »

Thank you. Collection and Dictionary objects seem quite similar. Is the main difference the speed with which a Dictionary item can be retrieved with the key?
Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 79676
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Dictionary and objects

Post by HansV »

I haven't tested whether dictionaries or collections are faster.

A dictionary has more features than a collection. For example, it has an Exists method that can be used to find out whether a specific key exists; this is more efficient than the looping that is required for collections. If you need this extra functionality, you should use a dictionary. If not, you're better off with a collection, since it's built into VBA and doesn't require setting a reference (and in some environments, Scripting is disabled because it might pose a security risk).
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Dictionary and objects

Post by agibsonsw »

Thanks again.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
rory
5StarLounger
Posts: 826
Joined: 24 Jan 2010, 15:56

Re: Dictionary and objects

Post by rory »

In addition to what Hans said, Dictionaries are supposed to be more lightweight and therefore faster in code. You can't insert items at a specific position though, so if you need that you should use a Collection.
Regards,
Rory

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Dictionary and objects

Post by agibsonsw »

That's helpful. Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.