Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

scripting.dictionary object confusion...

385 views
Skip to first unread message

Janak Sanariya

unread,
Jun 16, 2003, 3:19:27 PM6/16/03
to
The following code was written in VBA in excel but this seemed like the best
place to ask this question.

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

Han

unread,
Jun 16, 2003, 5:34:24 PM6/16/03
to
Hi Janak

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

Richard Mueller [MVP]

unread,
Jun 16, 2003, 5:50:45 PM6/16/03
to
Janak Sanariya wrote:

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


Richard Mueller [MVP]

unread,
Jun 16, 2003, 6:31:04 PM6/16/03
to
Hi,

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

Michael Harris (MVP)

unread,
Jun 16, 2003, 8:53:08 PM6/16/03
to
Janak Sanariya wrote:
> The following code was written in VBA in excel but this seemed like
> the best place to ask this question.
>
> 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)


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

Han

unread,
Jun 16, 2003, 9:15:31 PM6/16/03
to
Hi Richard

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

Han

unread,
Jun 16, 2003, 9:19:52 PM6/16/03
to
Hi Michael

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
>

Michael Harris (MVP)

unread,
Jun 16, 2003, 10:28:21 PM6/16/03
to
Han wrote:
> Hi Michael
>
> The d.item(ndex) doesn't work for me. Is that only my case???

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

Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp


Han

unread,
Jun 16, 2003, 10:32:59 PM6/16/03
to
Great as always !!!

"Michael Harris (MVP)" <mik...@mvps.org> wrote in message

news:#W2z5fHN...@TK2MSFTNGP11.phx.gbl...

0 new messages