ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   dictionary keys (https://www.excelbanter.com/excel-programming/301951-dictionary-keys.html)

Julio

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

Rob Bovey

dictionary keys
 
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" wrote in message
...
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




Jim Cone

dictionary keys
 
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" wrote in message ...
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" wrote in message
...
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



Julio

dictionary keys
 
thanks much

"Rob Bovey" wrote:

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" wrote in message
...
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





Bob Phillips[_6_]

dictionary keys
 
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)

"Rob Bovey" wrote in message
...
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" wrote in message
...
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






Bob Phillips[_6_]

dictionary keys
 
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)

"Jim Cone" wrote in message
...
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" wrote in message

...
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" wrote in message
...
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





Julio

dictionary keys
 
Bob,


it didn't work for me. Not unless i followed Rob's suggestion..

"Bob Phillips" wrote:

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)

"Rob Bovey" wrote in message
...
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" wrote in message
...
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







Bob Phillips[_6_]

dictionary keys
 
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)

"julio" wrote in message
...
Bob,


it didn't work for me. Not unless i followed Rob's suggestion..

"Bob Phillips" wrote:

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)

"Rob Bovey" wrote in message
...
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" wrote in message
...
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








Rob Bovey

dictionary keys
 
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 *


"Bob Phillips" wrote in message
...
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)

"Rob Bovey" wrote in message
...
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" wrote in message
...
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








Stephen Bullen[_3_]

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



Rob Bovey

dictionary keys
 
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" wrote in message
...
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





Stephen Bullen[_3_]

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




All times are GMT +1. The time now is 04:52 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com