Simple Question - Variable's as Sub names 
Author Message
 Simple Question - Variable's as Sub names

This is probably an easy one. Is there a way to call a Subroutine using a
variable? For example, if I have a variable RunSub = "MySubroutine", how can
I call "MySubroutine" using the variable name RunSub? Obviously, Call RunSub
doesn't work because it looks for the Subroutine called RunSub, not
MySubroutine. Thanks in advance for any assistance.



Sat, 22 Jul 2000 03:00:00 GMT  
 Simple Question - Variable's as Sub names

Stephen,

Quote:
>This is probably an easy one. Is there a way to call a Subroutine using a
>variable?

Not in VB5.

-- Jim Ferguson, FMS
   http://www.fmsinc.com



Sat, 22 Jul 2000 03:00:00 GMT  
 Simple Question - Variable's as Sub names

Stephen-
        How about something like this?
(You assign a value to Runsub and then
act according to what it is.)

If RunSub="MySubRoutine" Then
        Call MySubRoutine
End if
        Or as Case? Should work, right?
                                Joe
--
***********************************************************

Microsoft Developer MVP- Visual Basic
"The unity of freedom has never relied on
uniformity of opinion."  (John F. Kennedy)
**********************************************************
PS- Please reply to the newsgroup- except in the
case of flames, insults, etc. (Don't bother.)



Quote:
> This is probably an easy one. Is there a way to call a Subroutine using a
> variable? For example, if I have a variable RunSub = "MySubroutine", how
can
> I call "MySubroutine" using the variable name RunSub? Obviously, Call
RunSub
> doesn't work because it looks for the Subroutine called RunSub, not
> MySubroutine. Thanks in advance for any assistance.



Sat, 22 Jul 2000 03:00:00 GMT  
 Simple Question - Variable's as Sub names

Dear Stephen,

It is possible to declare a number of classes that could
contain the same method (with different implementation).
Then add them to some collection with meaning keys.
Then you could call these (different by content and the
same by interface) methods by the keys (in your case
it would be subroutine names).

For example;
===========================
<Class1>
Public Sub run_me(some_argument)
        MsgBox "It is Class1 - " & some_argument  
End Sub

<Class2>
Public Sub run_me(some_argument)
        MsgBox "It is Class2 - " & some_argument  
End Sub

<Form1>
Dim my_subs As Collection

Private Sub Form_Load()
Dim tmp_object As Object        

        Set my_subs=New Collection
        Set tmp_object = New Class1
        my_subs.Add tmp_object, "first_sub"
        Set tmp_object = New Class2
        my_subs.Add tmp_object,"second_sub"
End Sub

Private Sub Command1_Click()
        my_subs("first_sub").run_me "my argument"
        my_subs("second_sub").run_me 123.5
End Sub

=================================

It is only a rough idea. If you wanted to have quick access
to the collected methods it is possible to optimize this
sample. (Give the same interface to the classes and build
specialized collection to avoid late bounding.)

Ernest.



Quote:
> This is probably an easy one. Is there a way to call a Subroutine using a
> variable? For example, if I have a variable RunSub = "MySubroutine", how
can
> I call "MySubroutine" using the variable name RunSub? Obviously, Call
RunSub
> doesn't work because it looks for the Subroutine called RunSub, not
> MySubroutine. Thanks in advance for any assistance.



Sun, 23 Jul 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Automatic capitalization of variable names, type names, subs and functions

2. Public Sub with 'Reserved' Name

3. Calling a function/sub when a variable holds the name

4. Calling a sub name held in a variable

5. Help changing an object's name via a variable's value

6. Help: Variables lost in SUB's

7. 2 simple question about limit exceeded private sub

8. Help!--Displaying the Sub's Name

9. Passing as string variable from sub to sub

10. Calling a sub or function using a variable through another sub or function

11. Passing a sub name to another sub

12. Returning sub or function name within a specific sub or function

 

 
Powered by phpBB® Forum Software