Help in understanding “Dictionaries in VBA”. General advice, Pros and Cons
Hi, Just looking for some help to clear up a few things about “Dictionaries”
Aka having Fun with my Fuking Dik Object...Lol
.
Question _1) A more specific Question. What is the difference between Dictionary and Scripting.Dictionary ? (If there is one, probably is not lol )
To erlaberate: So I thought I knew what a Microsoft Scripting Runtime Dictionary ( MSRD) was and how to use it. ( Maybe I still do, lol.... **** ). I have used it quite a bit, mainly up to now by answering Threads using it as a way to learn about it.
I have a file from an OP I am attempting to help. The Exact File origin is unknown ( No point asking the OP as I needed 6 month to teach him how to spell VBA in English and he has no idea.... about.... well anything I think, but he is nice, I like him lol...
)
It uses a “Dictionary” a lot, that is to say makes a lot of New instances of a thing which goes by the name of Dictionary. This appears to work similarly to a Microsoft Scripting Runtime Dictionary.
So I am just trying to clear up if there is a subtle difference in the two, as I do see a minor difference in the Late binding use of it, or attempted use of it.
Below is referenced a simple demo code. Anyone with an idea about Microsoft Scripting Runtime Dictionary will see I think exactly what it is doing.
Rem 1 is a simple usage of a Microsoft Scripting Runtime Dictionary. (Scripting.Dictionary) ( I have it currently in Late binding, but it will work in early binding also if you comment out lines 80 and 90, and put back in lines 50 an 60 )
Rem 2 is the same again but using the Dictionary, which to all intents and purposes seems to work like a Microsoft Scripting Runtime Dictionary in Early binding.
( It is a standalone code so you can run it without further a do...)
_.....
So Dictionary seems to work to all intents and purposes like an Early binding-ed Microsoft Scripting Runtime Dictionary(Scripting.Dictionary). And sure enough it fails at line 180 if I take off ( Uncheck ) the reference to the library Microsoft Scripting Runtime
***** As often , _....
_...in carefully preparing my Post I may have the answer..... How’s this:
Is the answer no more than Excel somehow guesses right in line 180 and 190 of my demo code that I mean
Scripting.Dictionary ,
whereas for some strange reason it will not guess that in line 197 ? ( In this Line 197 I try to do the typical second line of late binding
= CreateObject("Scripting.Dictionary") ' No Problem here - it always works as I expect
_..But_...._..
= CreateObject("Dictionary") ' Run-time error '429': ActiveX component can't create object
_......_....._...._ Does not work!! ===Minor difference===
I note that after setting up Early binding, ( checking Microsoft Scripting Runtime in Extras, etc.. ) intellisense will offer me ( after I type = New ) both
.Scripting.Dictionary
and
.Dictionary
_ ... is this then that intellisense just offers what maybe / could be / perhaps, - but it is not an exact thing
So bottom line to question 1). Is this the answer: There is no difference. Just VBA Syntax being a bit inconsistent in how it handles the two “Words”, -- “Dictionary” and “Scripting.Dictionary” ?
_................::::..............
Question _2)Help in understanding “Dictionaries in VBA”. General advice, Pros and Cons
Is there really any advantage of using Dictonary , rather than for example a simple Array to do the same.
I can see that Maybe some of the Properties of a dictionary are very handy/ convenient.., and that Adding increases automatically the size of a Dictionary by a “row” / record. , using a simple If __ Exists, rather than a .Match attempt to check for uniqueness... Etc.. etc... etc...
There are lots of good references, for example.. ##
To do all those Dictionary Methods and Properties with an Array involves, amongst other things, the Re- Dim Preserve stuff., which can get messy! .. But I guess one could do all that with Functions (I have !!!! – and it was messy, LOL.. !), or even make a Class, call it a Dictionary and make your own Add method, and all the other Methods and Properties .
Is the point that a Dictionary references a Library and that telling me it uses something “outside” VBA that is particularly efficient, quick etc.??
I lack the experience or time and resources to do extensive checks on this in the case of the Dictionary, but occaisionally I have seen that with simpler things, the theoretical advantage such as efficiencies seem to have been lost as computer speed has got so good. It is not that I am so enthusiastic about writing and maintaining the complicated Functions i have done to do similar things, but if , at the end of the Day with ever increasing computer speed they work as well, then at least I can see a bit more of what is going on and feel more “_...In control.... “ “_...lol !”.. )
_................................................
Question _3) Am I correct in saying that if I make a Function Declared as a Dictionary, then I must Early Bind
because this:
Fuction Dik( ) As Scripting.Dictionary
Is pseudo doing this ( pseudoAlly ):
Dim Dik As ( New ) Scripting.Dictionary __ ????( this is the first line of a typical 2 line way of declaring a variable in Early Binding , the second line would then be in the Function)
_3)b).. ****EDIT: I have answered this..I think..
And that will only work when I Early bind.. as in my code lines from line 250.......
_...... Hmm .. I can late or Early bind within the Function though.... now there’s a Thought...
Thanks
Alan
****EDIT: I did a late Binding Function Also....( Edit Code from Line 300) so forget I Question _3
But then , following on from that.. Question 3b) instead
_ so then..
Question _3b) as I demo in my code... there is no reason to Dim ( or is there ?? ) a variable, say Dik, to Take from a Function returning a dictionary itself As a dictionary ?
I mean like this is OK to get a Dictionary from a function which returns a Dictionary
Dim Dik As Object
Set Dik = Fuk(___) ' In this case not New .. not a new instance
Where Fuk can be defined as an Object an within that Function a Dictionary is created through late binding
Question _C(ii) The thing receiving the Dictionary returned from a Function creating a Dictionary can be Dim ed As an Object. But I guess it is better practice generally to Dim that as a Dictionary ( .... or Scriptiung.Dictionary....
.. : )
Just a last clarity here to answer my last ( 3) ) question....The answer is... I do not need to Dim a Function returning a Dictionary as Scripting.Dictionary ( or Dictionary )... ( but I guess it is probably more efficient to do that.. ?? )
_.................................
Demo Code Here:
http://www.excelforum.com/showthread...70#post4431170
Thanks again for reading
Alan
Bookmarks