Strange nested dictionary behaviour

DHS100

Board Regular
Joined
May 21, 2006
Messages
149
Hi. I'm trying to check for the existance of nested dictionaries. It seems the very act of checking for them creates a new key?

Public Sub Test()
Dim dic As Dictionary
Set dic = New Dictionary

Debug.Print dic.Count 'Gives 0
On Error Resume Next
Debug.Print dic("test").Exists
Debug.Print dic.Count 'Gives 1?
End Sub

Is this meant to happen?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
This is something new to me. If you try to display any non-existing key also like Debug.Print dic(1), the count added one more. If you loop the routine, the count will no more increases :unsure:
 
Upvote 0
You're code is not checking for the existence of nested dictionaries, in fact it doesn't even work.
When you use dic("Test") it will automatically add the key "Test" if it doesn't already exist.
 
Upvote 0
You're code is not checking for the existence of nested dictionaries, in fact it doesn't even work.
When you use dic("Test") it will automatically add the key "Test" if it doesn't already exist.
I should have pointed out the code above is not the code that checks the nested dics, that is more like "If Not mDic(S(0))(S(1)).Exists(S(2)) Then". I was unaware that dictionaries had this aspect to them so was getting unexpected results in my old code. The code above was just to demonstrate.
 
Upvote 0
In order to ensure it doesn't add a new key you can use
VBA Code:
Public Sub Test()
Dim dic As Object
Set dic = CreateObject("scripting.dictionary")

Debug.Print dic.count 'Gives 0
'On Error Resume Next
dic.Add "test", CreateObject("scripting.dictionary")
Debug.Print dic.Item("test").Exists("abc")
dic.Item("test").Add "abc", CreateObject("Scripting.dictionary")
Debug.Print dic.Item("test").Exists("abc")
Debug.Print dic.Item("test").Item("abc").Exists("xyz")
Debug.Print dic.count 'Gives 1?
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top