After creating a new dictionary:
创建新词典后:
Dim teams as Dictionary
Set teams = New Dictionary
I noticed that it already contains empty key - value pair (teams.Count returns value of 1). How can I prevent this from happening or delete this pair? Is this normal behaviour?
我注意到它已经包含空键 - 值对(teams.Count返回值1)。如何防止这种情况发生或删除?这是正常的行为吗?
2 个解决方案
#1
2
If you are referencing the same Microsoft Scripting Runtime
dictionary scrrun.dll
as me - Excel 2010.
如果您引用与我相同的Microsoft Scripting Runtime字典scrrun.dll - Excel 2010。
Then the below code returns 0
as the .Count
of items after creation with no manipulation.
然后下面的代码在创建后没有操作的情况下返回0作为项目的.Count。
Sub SD()
Dim teams As Dictionary
Set teams = New Dictionary
Debug.Print teams.Count
End Sub
I wouldn't know the reason why your dictionary returns 1
, however I think a workaround it would be to simply use the .RemoveAll
method of the Dictionary
object
我不知道你的字典返回1的原因,但我认为解决方法是简单地使用Dictionary对象的.RemoveAll方法
Sub SD()
Dim teams As Dictionary
Set teams = New Dictionary
teams.RemoveAll
Debug.Print teams.Count
End Sub
#2
10
I had the same experience and solved it.
我有同样的经历并解决了它。
I had a Watch expression set for a value in the dictionary. Somehow this kept the empty key/value pair in the dictionary (or kept readding it).
我在字典中为一个值设置了一个Watch表达式。不知何故,这保留了字典中的空键/值对(或保持读取它)。
Removing the watch(es) and stepping through the code I now see the dictionary does not have the Empty/Empty item any longer.
删除手表并单步执行代码我现在看到字典不再有空/空项目了。
#1
2
If you are referencing the same Microsoft Scripting Runtime
dictionary scrrun.dll
as me - Excel 2010.
如果您引用与我相同的Microsoft Scripting Runtime字典scrrun.dll - Excel 2010。
Then the below code returns 0
as the .Count
of items after creation with no manipulation.
然后下面的代码在创建后没有操作的情况下返回0作为项目的.Count。
Sub SD()
Dim teams As Dictionary
Set teams = New Dictionary
Debug.Print teams.Count
End Sub
I wouldn't know the reason why your dictionary returns 1
, however I think a workaround it would be to simply use the .RemoveAll
method of the Dictionary
object
我不知道你的字典返回1的原因,但我认为解决方法是简单地使用Dictionary对象的.RemoveAll方法
Sub SD()
Dim teams As Dictionary
Set teams = New Dictionary
teams.RemoveAll
Debug.Print teams.Count
End Sub
#2
10
I had the same experience and solved it.
我有同样的经历并解决了它。
I had a Watch expression set for a value in the dictionary. Somehow this kept the empty key/value pair in the dictionary (or kept readding it).
我在字典中为一个值设置了一个Watch表达式。不知何故,这保留了字典中的空键/值对(或保持读取它)。
Removing the watch(es) and stepping through the code I now see the dictionary does not have the Empty/Empty item any longer.
删除手表并单步执行代码我现在看到字典不再有空/空项目了。