VBA:是什么原因导致传递给ParamArray的此字符串参数更改为一个数字(可疑地看起来像一个指针)? [英] VBA: What is causing this string argument passed to ParamArray to get changed to a number (that looks suspiciously like a pointer)?

查看:84
本文介绍了VBA:是什么原因导致传递给ParamArray的此字符串参数更改为一个数字(可疑地看起来像一个指针)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终它确实确实是编译器错误-请参阅接受的答案.

FINAL It does indeed appear to be a compiler bug - see the accepted answer.

在Excel 2007中使用VBA,在"Class1"中有以下代码:

Using VBA within Excel 2007, I have the following code in 'Class1':

Option Explicit

Public Function strange(dummy As String, ParamArray pa())
    Debug.Print pa(LBound(pa))
End Function

Public Sub not_strange(dummy As String, ParamArray pa())
    Debug.Print pa(LBound(pa))
End Sub

Public Function also_not_strange(ParamArray pa())
    Debug.Print pa(LBound(pa))
End Function

和模块中的一些模式代码:

and some mode code in a module:

Option Explicit

Public Function not_strange_either(dummy As String, ParamArray pa())
    Debug.Print pa(LBound(pa))
End Function

Public Sub outer(v)
    Dim c As Class1
    Set c = New Class1

    Call c.strange("", v(LBound(v)))
    Call c.not_strange("", v(LBound(v)))
    Call c.also_not_strange(v(LBound(v)))

    Call not_strange_either("", v(LBound(v)))
End Sub

如果从立即"窗口中调用外部",如下所示:

If call 'outer' from the Immediate window like this:

call outer(array("a"))

我得到的输出看起来很奇怪:

I get back output that seems strange:

 102085832 
a
a
a

被调用的例程是否在类模块中,它是Sub还是Function,以及是否有初始参数,这似乎很重要.我是否缺少有关VBA应该如何工作的信息?有什么想法吗?

It seems to matter whether the called routine is in a class module or not, whether it is a Sub or a Function, and whether or not there is an initial argument. Am I missing something about how VBA is supposed to work? Any ideas?

这个奇怪的数字从一个运行到另一个运行.我说可疑地看起来像一个指针",因为如果我这样称呼:

The strange number changes from run to run. I say "looks suspiciously like a pointer" because if I call this:

Public Sub outer2(v)
    Dim c As Class1
    Set c = New Class1

    Dim ind As Long
    For ind = LBound(v) To UBound(v)
        Call c.strange("", v(ind))
    Next ind
End Sub

像这样:

call outer2(array("a","b","c"))

我得到如下输出:

 101788312 
 101788328 
 101788344 

让我感到怀疑的是16的增量,但我真的不知道.另外,通过调用以下内容来传递值:

It's the increment by 16 that makes me suspicious, but I really don't know. Also, passing a value, say by calling:

Call c.strange("", CStr(v(ind)))

工作正常.

更多信息...如果我将'c.strange'的返回值分配给某个东西而不是扔掉它,我将得到相同的行为:

A little more info...If I assign the return value from 'c.strange' to something instead of throwing it away, I get the same behavior:

Public Sub outer3(v)
    Dim c As Class1
    Set c = New Class1

    Dim x
    x = c.strange("", v(LBound(v)))

    Call c.not_strange("", v(LBound(v)))
    Call c.also_not_strange(v(LBound(v)))

    Call not_strange_either("", v(LBound(v)))
End Sub

有趣的是,如果我像上面那样调用我的测试例程,并使用调用"Array"产生的参数,则假定指针的值会发生变化.但是,如果我这样称呼它:

Interestingly, if I call my test routines as above, with an argument that results from calling 'Array', the supposed-pointer value changes. However, if I call it like this:

call outer([{1,2,3}])

即使我反复打过电话,我也会得到相同的号码. (如果我切换到Windows上的另一个应用程序(例如浏览器),则数字会更改.)因此,现在我很感兴趣,Excel评估程序(带有方括号)似乎在缓存其结果...

I get back the same number, even if I make the call repeatedly. (The number changes if I switch to another app in Windows, like my browser.) So, now I'm intrigued that the Excel evaluator (invoked with the brackets) seemingly caches its results...

推荐答案

现在真棒.

转载于Office2003.
看起来像是编译器错误.

Reproduced on office 2003.
Looks like a compiler bug.

问题出在这一行:

Call c.strange("", v(LBound(v)))

在这里,编译器将创建一个Variant,其中包含一个Variant的一维数组,该数组的唯一元素是指针而不是值.然后,该指针转到strange函数,这实际上并不奇怪,它仅打印传递给它的Variant\Long值.

Here the compiler creates a Variant that holds a 1D array of Variant's, the only element of which is a pointer instead of the value. This pointer then goes to the strange function which actually is not strange, it only prints the Variant\Long value passed to it.

此技巧使编译器恢复正常:

This trick brings the compiler sanity back:

Call c.strange("", (v(LBound(v))))


编辑


EDIT

是的,这个幻数是指向应该传递给strange方法的VARIANT结构的指针.第一个字段是8,它是vbString,数据字段包含指向实际字符串"a"的指针.

Yes, this magic number is a pointer to the VARIANT structure which is supposed to be passed to the strange method. The first field of which is 8, which is vbString, and the data field contains a pointer to the actual string "a".

因此,这绝对是一个编译器错误...关于数组的又一个VB编译器错误;)

Therefore, it is definitely a compiler bug... Yet another VB compiler bug in regard of arrays ;)

这篇关于VBA:是什么原因导致传递给ParamArray的此字符串参数更改为一个数字(可疑地看起来像一个指针)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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