奇怪的观察:
通常,当我想将地址保存到变量中的函数时,我会执行以下操作:
Function getAddress(ByVal func As LongPtr) As LongPtr
getAddress = func
End Function
Sub printAddress()
Dim functionPointer As LongPtr
functionPointer = getAddress(AddressOf myFunc)
Debug.Print functionPointer
End Sub
但是我刚刚发现我可以使用 1-liner
functionPointer = VBA.CLngPtr(AddressOf myFunc)
尽管
functionPointer = CLngPtr(AddressOf myFunc)
...不起作用并引发
编译错误:
预期:表达式
这是怎么回事?据我所知,唯一的区别是它CLngPtr
是在全局变量(类?)中声明的,而VBA.CLngPtr
它是明确限定的,但我不知道为什么这会导致观察到的行为(它们都指向同一个函数)他们?)