Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining a string to form a subroutine name 1

Status
Not open for further replies.

BasicBoy

Programmer
Feb 22, 2008
156
ZA
I wish to direct the program to a subroutine which name I do not know beforehand - so during runtime I make up this name.
How can I make up a name and use it in the call function like in :
I have two subroutines called AddIntegers and AddSingle for adding integers and single numbers in separate routines.
During runtime I would test the number and if it is an integer I wish to direct execution to the subroutine called AddIntegers like in

If number is integer run AddIntegers :

SubRoutineName="Add" & "Integers"
Call SubRoutineName

Can anyone help

Thanks
 
Presumably the subroutines do already exist? In which case all you need do is a lookup, something like ...


SubRoutineName="Add" & "Integers"
Select Case SubRoutineName
Case "AddIntegers"
AddIntegers
Case "MultiplySingles"
MultiplySingles
[etc.]
End Select
 
Hi strongm
My idea is to write a short routine to handle the 60 possibilities I have - and I could have done this with a select function without resorting to the naming stuff lookup.
I need to go to a name that can be any of 60 choices and then go directly to that routine without a long select or if/then routine.
 
>and I could have done this with a select function without resorting to the naming stuff lookup

Indeed. That was what I was going to say next.

VB6 does not have a direct way of calling subroutines by their string name. There are ways of sort of achieving it, but they are more convoluted and involved (and slower) than a simple, efficient Select satetment
 
VB6 has a CallByName function that you might find useful.

There are some limitations that you should be aware of. For example, you cannot use CallByName to call a function that is in a regular (.bas) module. You can, however, call methods of a form and/or class module.

To see how this works, open a new VB project. But a single button on the form, and this code.

Code:
Option Explicit

Private Sub btnCommand1_Click()
    
    Dim DataType As String
    Dim Action As String
    
    Action = "Multiply"
    DataType = "Singles"
    
    CallByName Me, Action & DataType, VbMethod, 10, 2
    
End Sub

Public Function AddIntegers(ByVal A As Integer, ByVal B As Integer) As Integer
    
    AddIntegers = A + B
    
End Function

Public Function MultiplySingles(ByVal A As Single, ByVal B As Single) As Single
    
    MultiplySingles = A * B
    
End Function

When you click the button on the form, the call by name call will run the MultipleSingles finction with the parameters 10 & 2.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Just out of curiosity, why would you consider this convoluted?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
You have already pointed out one of the limitations: the function/methods has to be in a class.

And do you really think that:

result = CallByName(Me, Action & DataType, VbMethod, 10, 2)

is at least as clear as

result = MultiplySingles(10, 2)

I'd argue not.

And it is also dramatically slower than calling the function directly. It's even slower than calling the method on the object directly (MyObject.MultiplySingles), even a late bound object. This of course may not be an issue if the routine is only being called relatively few times.

And it has certain limitations in what parameters you can pass to it, although the details are probably a little esoteric for this particular post

CallByName is useful, but in my opinion shouldn't be used simply as a way of avoiding typing.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top