Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default 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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default 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




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default 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






  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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







  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default 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







  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default 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




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default 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




  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default 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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Customizing a dictionary Grant Excel Worksheet Functions 3 September 10th 09 01:22 PM
How to remove a word from dictionary? robmirabile Excel Discussion (Misc queries) 7 December 8th 04 06:39 PM
dictionary function wandering mage Excel Programming 6 June 15th 04 04:42 PM
Dictionary Functions Wandering Mage Excel Programming 0 June 11th 04 04:01 AM
Dictionary objet Zwi Excel Programming 2 January 26th 04 01:57 PM


All times are GMT +1. The time now is 10:33 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"