tags:

views:

54

answers:

2

I have this algorithm that I want to implement on VB6.

Sub Main()
dim stringVal1 as string, stringVal2 as string
dim getOne as boolean

stringVal1 = "FunctOne"
stringVal2 = "FunctTwo"

if getOne then
    'Call Function with function name assigned to stringVal1 ... how to call the function here?**
else
    'Call Function with function name assigned to stringVal1 ... how to call the function here?**
end if

End Sub


Function FunctOne()
   Msgbox "I'm function one"
End Function

Function FunctTwo()
   Msgbox "I'm function two"
End Function

Can this be done in VB6?

+3  A: 

Generally, such code patterns point to errors in your software design.

In the rare cases where this is really needed, CallByName accomplishes this.

Example:

Call CallByName(Me, "NameOfFunction", vbMethod, arguments)
Konrad Rudolph
AFAIK it is the only way to do polymorphism in vb6
Andrey
@Andrey: no, you’re quite wrong: VB6 knows interfaces (http://stackoverflow.com/questions/2581087). Furthermore, VB6 supports late binding out of the box so you can just call methods on polymorphic objects (but this no longer works with `Option Explicit`).
Konrad Rudolph
how terribly i was wrong. still i hate VB6 :)
Andrey
@Konrad +1 for your answer and your comment. A minor point though: late-binding still works with `Option Explicit`?
MarkJ
@MarkJ: Does it? It’s been too long since I last used VB6, apparently. :-(
Konrad Rudolph
@Konrad So refreshing to hear someone say that! Even as a joke
MarkJ
A: 

It would help if you give more information about why you're needing to call functions by the string representation of their name. Could you not simply re-write your code like this:

If getOne Then 
    Call FuncOne()
Else 
    Call FuncTwo() 
End If
Jazza