使用晚期绑定而不是早期绑定的字典中的运行时错误 [英] Runtime Error with Dictionary when using late binding but not early binding

查看:142
本文介绍了使用晚期绑定而不是早期绑定的字典中的运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所做的是将字典放入子例程中的数组

What I do is putting a dictionary into an array within a Sub Routine

这是定义

Dim Arr() As Variant
ReDim Arr(0 To Dict.Count - 1)
For c= 0 To Dict.Count - 1 
   Arr(c) = Dict.Keys(c) ' <~~~~~~ Error here
Next c 

编译器说


运行时错误451:属性让程序未定义,而属性get Procedure没有返回对象。

Runtime Error 451: Property let procedure not defined and property get Procedure did not return an object.

它适用于

Public Sub SubRoutine(Dict As Scripting.Dictionary) 

但不与

Public Sub SubRoutine(Dict As Object) –

请参阅声明没有Microsoft Scripting Runtime的字典

推荐答案

< Solution>



你这样做

<Solution>

You do this

Dim Arr() As Variant
ReDim Arr(0 To Dict.Count - 1)
For c = 0 To Dict.Count - 1
    Arr(c) = Dict.Keys(c)
Next c

但是这样循环是没有必要的。这就是 Arr = Dict.Keys 。所以,而不是上述,只是做

But looping like this is quite unnecessary. That's what Arr = Dict.Keys does. So instead of the above, just do

Dim Arr As Variant
Arr = Dict.Keys

额外的好处是,这使得错误消失。

The added bonus is that this makes the error go away.

但是为什么错误发生在代码的后期版本中,但不是早期版本?

But why did the error occur in the late bound version of the code but not the early bound?

早期绑定,编译器知道 .Keys 方法没有参数 - 它只返回一个数组。所以它将 Dict.Keys(c)解释为 {returned array}(c)并返回 c 这个返回数组的元素。

With early binding, the compiler knows that the .Keys method takes no parameters — it just returns an array. So it interprets Dict.Keys(c) as {returned array}(c) and returns the cth element of that returned array.

使用 late 绑定,我猜对象容器不知道 .Keys 方法不接受参数(aka参数),所以它作为参数发送 c 。但是没有定义这样的getter(或setter),因此这个错误。要解决这个问题,您可以显然强制不向键发送参数方法通过说 Dict.Keys()(c),它带回 {返回数组}(c)行为。

With late binding, I guess the Object container doesn't know that the .Keys method doesn't take parameters (a.k.a. arguments), so it sends c to it as a parameter. But there is no such getter (or setter) defined, hence the error. To remedy, you can apparently force it to send no parameters to the Keys method by saying Dict.Keys()(c), which brings back the {returned array}(c) behaviour.

这是我第一次碰到这样的情况,后期绑定代码的行为与早期界限不同。

This is the first time I bump into a case like this where the late bound code behaves differently than the early bound.

这篇关于使用晚期绑定而不是早期绑定的字典中的运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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