Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

There's something about the Dictionary Object that I don't get... 2

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
Why does this code
Code:
Dim RegValue, RegValues, i 'Create some variables

Set RegValues = CreateObject("Scripting.Dictionary")

RegValues.Add "a", "Athens" 'Add some keys and items.
RegValues.Add "b", "Belgrade"
RegValues.Add "c", "Cairo"

RegValue = RegValues.keys 'Get the keys

wscript.echo "RegValues.Count -1 = " & RegValues.Count -1

For i = 0 To RegValues.Count -1 'Iterate the array
    wscript.echo "i = '" & i & "'" & vbCrLf & """" & RegValues(i) & """" 'Print key
Next
produce this output:
[tt]RegValues.Count -1 = 2
i = '0'
""
i = '1'
""
i = '2'
""[/tt]

when I'd have expected it to produce something like:
[tt]RegValues.Count -1 = 2
i = '0'
"a"
i = '1'
"b"
i = '2'
"b"[/tt]

My code is based upon code I got off t'internet so I've clearly edited it incorrectly...

Can someone help me understand how to manipulate the values stored in the Dictionary so I can do whatever I like with them, please?

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Because unlike a regular array, you get the values of a Dictionary by calling the name.

wscript.echo RegValues(0) does not work but
wscript.echo RegValues("a") in your case should.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Code:
Dim RegValue, RegValues, value1 'Create some variables

Set RegValues = CreateObject("Scripting.Dictionary")

RegValues.Add "a", "Athens" 'Add some keys and items.
RegValues.Add "b", "Belgrade"
RegValues.Add "c", "Cairo"

RegValue = RegValues.keys 'Get the keys

For Each value1 In RegValue
	WScript.Echo value1
	WScript.Echo RegValues(value1)
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks for you help, chaps.

As often happens, I worked out what was wrong and have corrected it:
Code:
Dim a, d, i 'Create some variables

Set d = CreateObject("Scripting.Dictionary")

d.Add "a", "Athens" 'Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

a = d.keys 'Get the keys
b = d.items 'Get The items
wscript.echo "count = " & d.count
For i = 0 To d.Count -1 'Iterate the array
    wscript.echo "a(i) = " & a(i) 'Print key
    wscript.echo "b(i) = " & b(i) 'Print item
Next

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Disregard my 8:34 post....this cold/flu must be affecting me...I should have stayed home.

The original code you posted works if you change RegValues(i) to RegValue(i)

You can retrieve the values for the keys and items in a similar manner as a regular array...normally though you get the value by its name rather than the index.

Seems you found the solution you wanted so that's what matters most.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top