Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define a function alias in VBA, possible?

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:

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?

like image 764
orshachar Avatar asked Nov 30 '10 10:11

orshachar


2 Answers

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

like image 118
Fionnuala Avatar answered Oct 05 '22 13:10

Fionnuala


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)

'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
like image 25
Alex K. Avatar answered Oct 05 '22 13:10

Alex K.