字典.存在返回可变键和常数键的不同值的字典 [英] Dictionary.Exists Returning Different Values for Variable and Constant Key

查看:98
本文介绍了字典.存在返回可变键和常数键的不同值的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用MS Scripting Runtime Library字典作为我当前正在从事的VBA项目的goto数据结构,但我遇到了一个令人沮丧的问题,这使我认为它们可能并不那么好就像我想的那样. .Exists方法似乎是一个非常有用的方法,但是当我尝试在For循环中使用它时,我会得到一些奇怪的行为.运行此代码时,我得到一些奇怪的行为:

I've been using MS Scripting Runtime Library dictionaries as my goto data structure for the VBA project I'm currently working on, and I just ran into a frustrating issue that is making me think that they might not really be as good as I thought they were. The .Exists method seems like a very useful method, but I get some weird behavior when I try to use it inside a For loop. I get some weird behavior when I run this code:

Sub TestExists()
Dim i As Long
Dim dRowsInEachCol As Dictionary
Set dRowsInEachCol = New Dictionary

Dim lColLen As Long, lColLenFreq As Long

For i = 0 To 100
    dRowsInEachCol.Add i, i
Next

Dim dColLenFreqs As Dictionary
For i = 0 To dRowsInEachCol.Count - 1

    lColLen = dRowsInEachCol.Items()(i)

    If i = 0 Then
        Set dColLenFreqs = New Dictionary
    End If
    Debug.Print dColLenFreqs.Exists(0), _
                dColLenFreqs.Exists(1), _
                dColLenFreqs.Exists(lColLen)

    If dColLenFreqs.Exists(lColLen) Then
         lColLenFreq = dColLenFreqs.Item(lColLen) + 1
    Else
        lColLenFreq = 1
    End If

'        'This will add a new item to dUniqColLengths _
'            w/ key = column length, or overwrite item w/ _
'            new column number if that key exists already _
'            (INTENDED result is unique col lengths only in dict):
    dColLenFreqs.Item(lColLen) = lColLenFreq
Next
End Sub

首先,当我创建字典

First, there seems to be a key without an item that is added implicitly when I create the dictionary (similar to the issue in this question). But can't just accept that issue for what it is and work around it, because I actually get different result for the Exists method when I check lColLen compared to a constant with the same value as lColLen. This is the state of the watch window right after the line Set dColLenFreqs = New Dictionary has been executed:

我试图切换到后期绑定并完全限定了字典变量声明,但这不能解决问题.

I have tried switching to late binding and fully qualifying my dictionary variable declarations and this did not fix the issue.

在相关的情况下,该函数的目的是接受字典输入(dRowsInEachCol是预期的输入,但我修改后在函数内创建字典以消除此问题并缩短了代码),然后返回字典,其中以dRowsInEachCol中的唯一项为键,以dRowsInEachCol中的每个唯一项的频率为项.

In case it's relevant, the purpose of the function is to take a dictionary input (dRowsInEachCol is the intended input, but I modified to create the dictionary within the function to eliminate this as the issue and shorten the code), and return a dictionary with the unique items in dRowsInEachCol as the keys, and the frequency of each unique item in dRowsInEachCol as the items.

我想知道:

  1. 这仅仅是Scripting.Dictionary对象中的错误,还是我缺少什么?
  2. 如果这是Scripting.Dictionary对象中的错误,那么有哪些解决方法可用?我已经阅读了与Mac兼容的字典类这个,但是我我担心这些问题还会引起其他故障.这些恐惧有根据吗?另一个想法是只切换到临时的excel范围或集合和数组,可能包装在一个类中.我也愿意接受其他建议.
  1. Is this just a bug in the Scripting.Dictionary object, or am I missing something?
  2. If it is a bug in the Scripting.Dictionary object, what workarounds are available? I have read about mac-compatible dictionary classes like this one and this one, but I'm worried these would cause other glitches. Are these fears founded? Another idea would be to just switch to temporary excel ranges or collections and arrays, possibly wrapped in a class. I'm open to other suggestions as well.

此先前的问题似乎也与我的问题有关,可能是可供参考.

This previous question also seems somewhat related to my issue and might be useful for reference.

不重要的编辑:由于

Unimportant This was marked as duplicate because of this question, and after reading the answer by Tim Williams, I can see why these questions are related, although the other question is different because it assumes Tim Williams's answer, and then asks why. I don't really have anything to add to my question to differentiate it further, and I agree that having a link to the other question is a helpful, so I'll leave it to the community to decide if it should be closed as duplicate.

推荐答案

无法在此处复制-在第一次通过循环的情况下,所有三个Exists监视值均为false.

Can't replicate here - on the first pass though the loop all three Exists watch values are false.

您还设置了哪些其他手表?可能有另一只手表改变了Exists结果:使用Dictionary对象时,请务必注意手表窗口.

What other watches do you have set though? It's possible another watch is altering the Exists result: you need to be careful with the watch window when using a Dictionary object.

例如看到:

已填充字典在监视窗口中检查字典项后,显示一个空项

怀疑后,在

dColLenFreqs.Item(lColLen) 

手表

dColLenFreqs.Exists(lColLen) 

现在输出True.

如果删除所有手表,仅查看Debug输出,您也会看到它与预期的一样.

If you delete all your watches and just look at the Debug output you'll also see it's as expected.

这篇关于字典.存在返回可变键和常数键的不同值的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆