在 VBA 中定义一个函数别名,可能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4312447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:21:56  来源:igfitidea点击:

define a function alias in VBA, possible?

vbafunction-pointersalias

提问by orshachar

I am using VBA behind MS Access Say I have global methods foo1 and foo2 that gets the same arguments but do different things. I know that in C++ I can assign a function an alias. Something like: instead of:

我在 MS Access 后面使用 VBA 假设我有全局方法 foo1 和 foo2,它们获取相同的参数但做不同的事情。我知道在 C++ 中,我可以为函数分配别名。类似于:而不是:

If (term) then
   foo1 arg1, arg2, arg3
else
   foo2 arg1, arg2, arg3
End If

I want to write:

我想写:

Var new_func = Iff (term, foo1,foo2)
new_func arg1, arg2, arg3

Can I do this on vba?

我可以在 vba 上做这个吗?

回答by Fionnuala

Would Run suit?

跑步适合吗?

new_func = IIf(term, "foo1", "foo2")
''new_func arg1, arg2, arg3

res = Run(new_func, "a", "b", 1)

More information: http://msdn.microsoft.com/en-us/library/aa199108(office.10).aspx

更多信息:http: //msdn.microsoft.com/en-us/library/aa199108(office.10).aspx

回答by Alex K.

If the 2 functions were implemented in 2 different class modules you could give them the same name & call them across an interface. That's about as close as you can get. (and its not very close)

如果这 2 个函数是在 2 个不同的类模块中实现的,您可以为它们指定相同的名称并通过接口调用它们。这几乎是你所能得到的。(而且它不是很接近)

'empty interface class, name: IFoo
Public Function TheFunc(i As Integer) As Long
'
End Function
'class clsFoo1
Implements IFoo

Private Function IFoo_TheFunc(i As Integer) As Long
MsgBox "I'm clsFoo1"
End Function
'class clsFoo2
Implements IFoo

Private Function IFoo_TheFunc(i As Integer) As Long
MsgBox "I'm clsFoo2"
End Function
'module code;
Dim myFoo As IFoo
If var = 1 Then
    Set myFoo = New clsFoo1
Else
    Set myFoo = New clsFoo2
End If

myFoo.TheFunc 12345

回答by Tom Juergens

No, unfortunately, that's not possible with VBA. Would be nice, but you'll just have to stick to your first syntax.

不,不幸的是,这在 VBA 中是不可能的。会很好,但你只需要坚持你的第一个语法。

Update: So, I stand by my original assertion that the construct proposed in the question does not exist in VBA, but Alex K.and Remouhave provided usable workarounds.

更新:所以,我坚持我最初的断言,即问题中提出的构造在 VBA 中不存在,但Alex K.Remou提供了可用的解决方法。

回答by Mark Mayo

Try this:

尝试这个:

http://oreilly.com/catalog/vbanut/chapter/booklet.html#sect5

http://oreilly.com/catalog/vbanut/chapter/booklet.html#sect5

The AddressOfoperator.

AddressOf运营商。

However as noted on http://discuss.fogcreek.com/joelonsoftware4/default.asp?cmd=show&ixPost=140196&ixReplies=19

但是,如http://discuss.fogcreek.com/joelonsoftware4/default.asp?cmd=show&ixPost=140196&ixReplies=19 所述

"AddressOf is really a cheap hack. VB doesn't support first-class function values like most other languages, but with AddressOf is supports it halfway. Yes you can get the address of a function but you can't invoke a function by address (unless that function is a message processor and only then with the Win32 function CallWndProc). All you can do to simulate this behavior is take generic dispatch objects instead of function pointers and ensure that the objects support the necessary functions. "

“AddressOf 确实是一个廉价的hack。VB 不支持像大多数其他语言一样的一流函数值,但是AddressOf 是支持它的一半。是的,您可以获得函数的地址,但不能通过地址调用函数(除非该函数是消息处理器,并且只有使用 Win32 函数 CallWndProc 才能模拟此行为。您所能做的就是采用通用调度对象而不是函数指针,并确保这些对象支持必要的功能。”

Unfortunately, that's about as close as you'll get, I believe.

不幸的是,我相信这几乎是你所能得到的。

For more info on AddressOf, see here.

有关更多信息AddressOf,请参见此处

回答by Sancarn

I realise this is an old question, but I suggest using STD.Types.Callbackwhich is part of my VBA library.

我意识到这是一个老问题,但我建议使用STD.Types.Callback,它是我的 VBA 库的一部分。

Usage:

用法:

sub test()
  Dim msg as STD_Types_Callback
  set msg = STD.Types.Callback.Create("Module","Module1","Msg")
  'or
  'set msg = STD.Types.Callback.Create("Object",myObject,"Msg")

  DoMessage(msg, "hello world") '=> "hello world"
end sub


function Msg(message)
  Msgbox message
end function

function DoMessage(a as object, b as string)
  a(b)
end function