dictionary keys

J

julio

How do you access dictionary keys? What's wrong with this? (I get runtime error "let not defined, get doesn't return object" or the other way around)

Sub Macro1()

Dim dic As Object
Dim s As String

Set dic = CreateObject("Scripting.Dictionary")

dic.Add "foo", "bar"

s = dic.keys(0)

End Sub
 
R

Rob Bovey

Hi Julio,

What you have ought to work in theory, but I get the same error you do.
It seems like the Dictionary object doesn't like late binding. If I set a
reference to the Microsoft Scripting Runtime and change the code to use
early binding as shown below it works as expected.

Sub Macro1()

Dim dic As Scripting.Dictionary
Dim s As String

Set dic = CreateObject("Scripting.Dictionary")

dic.Add "foo", "bar"

s = dic.Keys(0)

End Sub

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


julio said:
How do you access dictionary keys? What's wrong with this? (I get runtime
error "let not defined, get doesn't return object" or the other way around)
 
J

Jim Cone

Rob,

I thought that the Keys method returned a variant array and that
the only way to return a key was by using the following???

Dim S as Variant
dic.Add "foo", "bar"
S = dic.Keys
MsgBox S(0)

Regards,
Jim Cone
San Francisco, CA

Rob Bovey said:
Hi Julio,

What you have ought to work in theory, but I get the same error you do.
It seems like the Dictionary object doesn't like late binding. If I set a
reference to the Microsoft Scripting Runtime and change the code to use
early binding as shown below it works as expected.
Sub Macro1()
Dim dic As Scripting.Dictionary
Dim s As String
Set dic = CreateObject("Scripting.Dictionary")
dic.Add "foo", "bar"
s = dic.Keys(0)
End Sub
 
J

julio

thanks much

Rob Bovey said:
Hi Julio,

What you have ought to work in theory, but I get the same error you do.
It seems like the Dictionary object doesn't like late binding. If I set a
reference to the Microsoft Scripting Runtime and change the code to use
early binding as shown below it works as expected.

Sub Macro1()

Dim dic As Scripting.Dictionary
Dim s As String

Set dic = CreateObject("Scripting.Dictionary")

dic.Add "foo", "bar"

s = dic.Keys(0)

End Sub

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *



error "let not defined, get doesn't return object" or the other way around)
 
B

Bob Phillips

Rob,

Help gives all its dictionary examples using late binding, and this works

Dim dic As Object
Dim s As String
Dim a

Set dic = CreateObject("Scripting.Dictionary")

dic.Add "foo", "bar"

a = dic.keys
s = a(0)

Weird or what?


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
B

Bob Phillips

Jim,

That is the same results as I found, but it's not the same as the OP and Rob
tried. They tried to access the keys array directly, and failed. This is so
even if you declare s as variant in the OP's code. Whereas Rob accessed in
directly with early binding.

Still not clear why you cannot directly access the keys array with late
binding.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
B

Bob Phillips

Julio,

That's odd. Just tried it again, and this code still works for me

Sub Macro1()

Dim dic As Object
Dim s As Variant

Set dic = CreateObject("Scripting.Dictionary")

dic.Add "foo", "bar"

Dim a
a = dic.keys
MsgBox a(0)

End Sub

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
R

Rob Bovey

Hi Bob,

Yeah that is really strange. I can't imagine what's going on under the
hood there.

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *
 
S

Stephen Bullen

Hi Rob,
Yeah that is really strange. I can't imagine what's going on under the
hood there.

I guess there's an optional parameter on the Keys property, so we can change an item's Key value:

Public Property Let Keys(Optional vOldKey As Variant, vNewKey As Variant)
End Property

Public Property Get Keys(Optional vOldKey As Variant)
If Not IsMissing(vOldKey) Then
Err.Raise "Not Implemented"
End If

End Property

So when late-bound, the interpreter is passing the index to the property (which gives an error from within the property), but when early-bound I
think there's a check that the compiler can do to see if a property is implemented, so it sees that the Keys Property Get doesn't in fact implement
a parameter, so knows that the (0) is an index into the array returned by the Keys() property.

Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.ie
 
R

Rob Bovey

Hi Stephen,

I don't know. Keys is a method not a property, and it's documented to
return an array of Keys, so the syntax Dictionary.Keys(Index) should work in
either case, returning the Index item from the array. Why it pukes when used
directly that way with late-binding but has no problem assigning its array
to a Variant still doesn't make sense to me.

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


Stephen Bullen said:
Hi Rob,


I guess there's an optional parameter on the Keys property, so we can change an item's Key value:

Public Property Let Keys(Optional vOldKey As Variant, vNewKey As Variant)
End Property

Public Property Get Keys(Optional vOldKey As Variant)
If Not IsMissing(vOldKey) Then
Err.Raise "Not Implemented"
End If

End Property

So when late-bound, the interpreter is passing the index to the property
(which gives an error from within the property), but when early-bound I
think there's a check that the compiler can do to see if a property is
implemented, so it sees that the Keys Property Get doesn't in fact implement
 
S

Stephen Bullen

Hi Rob,
I don't know. Keys is a method not a property, and it's documented to
return an array of Keys, so the syntax Dictionary.Keys(Index) should work in
either case, returning the Index item from the array. Why it pukes when used
directly that way with late-binding but has no problem assigning its array
to a Variant still doesn't make sense to me.

D'Oh! So it is - I was combining the Key and Keys and coming up with 6!

Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.ie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top