Why does the following peice of code not work?
(I get runtime error '451'. Property let procedure not defined and property
get procedure did not return an object.)
The is on the line Debug.print....
Sub Macro2()
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "aheader"
d.Add "b", "bheader"
d.Add "c", "cheader"
For ndex = 0 To 2
Debug.Print d.items(ndex)
Next
End Sub
But this piece does!?!
Sub Macro2()
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "aheader"
d.Add "b", "bheader"
d.Add "c", "cheader"
a = d.items
For ndex = 0 To 2
Debug.Print a(ndex)
Next
End Sub
I don't understand why the A variable is needed or why it changes anything.
-J
You may want,
for each d1 in d
debug.print d(d1)
next
*Personally* I think the dictionary is incomplete technology and deprecated
in the middle.
"Janak Sanariya" <jan...@targetanalysis.com> wrote in message
news:evyU6wDN...@TK2MSFTNGP11.phx.gbl...
Hi,
A dictionary object is an associative array of (key, item) pairs. It is
unlike other arrays in that you cannot get at the elements by an index
number. If you want to enumerate all elements in the dictionary object, or
to address them by an index number, then you have to convert the dictionary
object to an array as you did in the second example. The power of this
object is that it automatically guarantees unique keys, and it quickly finds
the item that corresponds to any key. It can also be used to check for the
existence of a key without the need to enumerate.
Sub Macro2()
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "aheader"
d.Add "b", "bheader"
d.Add "c", "cheader"
Debug.Print d("a")
If d.exists("c") then
Debug.Print "Key c has value " & d("c")
End If
End Sub
--
Richard
Microsoft MVP Scripting and ADSI
http://www.rlmueller.net
--
Han shows me to be wrong - you can enumerate a dictionary object. This is
also documented in the WSH 5.6 documentation. You enumerate it as a
collection, not an array.
--
Richard
Microsoft MVP Scripting and ADSI
http://www.rlmueller.net
--
"Han" <hp4...@kornet.net> wrote in message
news:uTGlQ8EN...@TK2MSFTNGP10.phx.gbl...
The line above should be:
Debug.Print d.item(ndex)
The Items() function returns any array of the items stored in the
dictionary.
The Item(index) indexed property accessor method returns the item at the
corresponding index where the index could be the item's numeric zero-based
position or its key.
> Next
> End Sub
>
> But this piece does!?!
> Sub Macro2()
> Set d = CreateObject("Scripting.Dictionary")
> d.Add "a", "aheader"
> d.Add "b", "bheader"
> d.Add "c", "cheader"
> a = d.items
> For ndex = 0 To 2
> Debug.Print a(ndex)
> Next
> End Sub
>
> I don't understand why the A variable is needed or why it changes
> anything.
>
> -J
--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
Technet Script Center
http://www.microsoft.com/technet/scriptcenter/default.asp
Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp
Strange statement. Why can it be wrong, enumerating as a collection, not an
array? Isn't it a personal taste or another option? Is there any general
benefit to use array by adding one more line compared with using existing
collection?
Though not related, by the last comment "imcomplete", I mean interface. I
think msgbox dic.items(0) should work as array(1,2,3)(0) does.
"Richard Mueller [MVP]" <rlmu...@ameritech.net> wrote in message
news:u#ZxzjFND...@TK2MSFTNGP11.phx.gbl...
The d.item(ndex) doesn't work for me. Is that only my case???
"Michael Harris (MVP)" <mik...@mvps.org> wrote in message
news:eHUGtqGN...@TK2MSFTNGP10.phx.gbl...
> Microsoft?Windows?000 Scripting Guide
> http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp
>
Try d.items()(ndex)
Logically it's the same as (but far less efficient than):
a = d.items
For ndex = 0 To 2
Debug.Print a(ndex)
Next
Access via the item propery with a numeric index only works it the items
were actually added with zero-based numeric keys.
So amend my earlier claim that with Item(index) that index could be the
zero-based position which is in fact not true.
I tend to use dictionaries as buffers with references like
sdBuf(sdBuf.count) = "some string"
sdBuf(sdBuf.count) = "another string"
sdBuf(sdBuf.count) = "yet another string"
usually in the context of build up HTML so my keys always numeric values
that correspond to 0-based position.
--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
Technet Script Center
http://www.microsoft.com/technet/scriptcenter/default.asp
Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp
"Michael Harris (MVP)" <mik...@mvps.org> wrote in message
news:#W2z5fHN...@TK2MSFTNGP11.phx.gbl...