6

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?

5 Answers 5

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

I personally don't really like Run much, but yes, at least this solution will work, so you get my vote.
4

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

4 Comments

Interfaces in VBA? That's new to me... I mean, yes, you can use existing interfaces, but you can't define your own and make classes implement them, which would be needed here. Or have I missed something?
Sure, If its a class module with no implementation VBA can treat it as an interface & any other class may implement its public members, example updated.
Very clever. Thanks for the update! Not doing much in VBA anymore these days, but this trick just might come in handy one day. Still seems a little convoluted for OP.
Trivia: Implementing Interfaces is not supported in VBA 5 (Office 97), but is in VBA 6 (Office 2000 to 2007 I think) and presumably VBA 7 (Office 2010)
1

Try this:

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

The AddressOf operator.

However as noted on 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. "

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

For more info on AddressOf, see here.

3 Comments

Clever idea, but I don't (yet) see how AdressOf will help here, since it can't be used to call a function within VBA.
It was more a 'try this to see the limits of VBA' link, unfortunately. AddressOf and calling same-named functions across an interface (if other poster is correct) are about as close as you'll get.
As a terrible hack you can actually use CallWindowProc to call any VBA addressof pointer so long as the signature matches & you use 0 for the hwnd. You can even pass As String.
1

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

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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.