Calling a sub / function which name is built at runtime

Hi ,

is it possible to call a sub / function in VB if I build its name at runtime?
Let's say I have 3 subs  like

Private sub s1() 'or s2() or s3()
'do some stuff
End sub

and I have a string var MyStr which could have a value of "s1","s2" or "s3"

How can I call a sub which name is the same as MyStr value

I know I can do something like
If MyStr = "s1" Then
Call s1
Else
......
End If

but I don't want this.
I want to call a sub / fuction which name is defined at runtime as a string(or something else).
BTW is there in VB something similar to javascript's 'eval' functionality? The answer seems to be No, but anyway :)
hveldAsked:
Who is Participating?
 
jklmnConnect With a Mentor Commented:
Hi hveld,

This can do what you want, though you might not like:-)
You need to put your Subs into the Script:

Private Sub Command1_Click()
Dim MyStr As String
MyStr = "s1"
ScriptControl1.Run "s1"
End Sub

Private Sub Form_Load()
Dim s As String
s = "sub s1()" & vbCrLf & _
"msgbox ""this is sub s1()""" & vbCrLf & _
"end sub" & vbCrLf
ScriptControl1.AddCode s
s = "sub s2()" & vbCrLf & _
"msgbox ""this is sub s2()""" & vbCrLf & _
"end sub" & vbCrLf
ScriptControl1.AddCode s
End Sub
0
 
jklmnCommented:
Hi,

in VB you can do
Print ScriptControl1.Eval(2 + 3)
Is this what you want in you 'BTW'?
0
 
jklmnCommented:
Ooops, I meant

Print ScriptControl1.Eval("2 + 3")
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Suat OzgurWeb / Application DeveloperCommented:
jklmn is right. But you need MSscript control. You can download it from MS site.

Then you can create runtime sub/functions and also use that great Eval method like jklmn said.

suat
0
 
jklmnCommented:
To smozqur:

Microsoft Script Control 1.0 should be available in VB6 from Components dialog box.
In fact, I didn't answer the first part of the question, can you answer that? I am listening....
0
 
jklmnCommented:
Sorry, that should be
ScriptControl1.Run MyStr
0
 
rovermCommented:
Hi!

I think it is impossible to do that, maybe with API's or subclassing it's possible...but I think not.

Maybe it is possible to use something like
AddressOf MyStr and then call that ?

Looking into it...

D'Mzzl!
RoverM
0
 
Suat OzgurWeb / Application DeveloperCommented:
Private Sub Command1_Click()
Dim Mystr As String
Mystr = "s1" 'The name of the sub in string variable
Call MainCode(Mystr)
End Sub

Private Sub MainCode(strSubName As String)
ScriptControl.AddCode ("Private Function RunSub()" & vbCrLf & "'Call (" & strSubName & ")" & vbCrLf & "Msgbox ""subname=" & strSubName & """" & vbCrLf & "End Function")
ScriptControl.Eval ("RunSub(" & Mystr & ")")
End Sub

sure
0
 
Suat OzgurWeb / Application DeveloperCommented:
I am sorry. Wrong window :)

suat
0
 
Suat OzgurWeb / Application DeveloperCommented:
No no, right window :) twice.

suat
0
 
Suat OzgurWeb / Application DeveloperCommented:
Oh, similar but jklmn is fastest.

Congratulations

suat
0
 
rovermCommented:
Wow ! Didn't think of that!
Way to go jklmn and suat (or is it sure now :>)!!

D'Mzzl!
RoverM
0
 
Suat OzgurWeb / Application DeveloperCommented:
"sure" was for ", can you answer that? I am listening...." of jklm.

:)
suat
0
 
jklmnCommented:
To smozqur,

Sorry, but I really want something else from you or others. The posted aproach might not be accepted by the questioner since it need to put all Subs, Functions (with 'vbCrLf's) into the Script Control. Another aproach that the questioner doen't like is simpler:
If MyStr = "s1" Then
Call s1
Else....

To roverm,

>AddressOf MyStr and then call that ?
That is for some APIs, can we do that in the code we written?




0
 
rovermCommented:
jklmn:
I don't know, I used this when I build a project with several API timers. The SetTimer API calls an module.function/sub by using AddressOf.
I know that AddressOf can get the address of a Sub, Function or a Property. But I don't know if we can use this with a string or how to call it.

hveld:

Why don't you want to use something like:
Select Case MyProc
Case "S1"
  Call S1
Case "S2"
  Call S2
Case Else
  Call S3
End Select

D'Mzzl!
RoverM
0
 
Éric MoreauSenior .Net ConsultantCommented:
If S1, S2 and S3 are public methods of an object (a form for example), you can use CallByName to call them.
0
 
pierrecampeCommented:
yes say for example you have a form 'Form1' with in it a sub 'public sub S1()'
then you can call it like so:
CallByName Form1, "S1", VbMethod
if the sub is 'public sub S2(parameter)'
then you can call it like so:
a = CallByName(Form1, "S2", VbMethod, parameter)
myvariable=parameter
note that 'parameter' is a variant
0
 
rovermCommented:
The best answers can be soooooooo easy....:>

Way to go emoreau!

D'Mzzl!
RoverM
0
 
Suat OzgurWeb / Application DeveloperCommented:
I agree...

suat
0
 
pierrecampeCommented:
Hi roverm
D'Mzzl!
is EE dan vergeven van de hollanders ?
geintje :-)
en natuurlijk van een belg

0
 
rovermCommented:
Ik denk het ! ;-)
Pierre: You're the second to understand my signature!

D'Mzzl!
RoverM
0
 
hveldAuthor Commented:
Thanx jklmn!

I din't know you can build entire functions as strings at runtime and execute them, this will help me a lot.
Of course pierrecampe's answer seems to be the best and simpliest one for my question (as I asked it) , but this will do a much better job for what i'm trying to do, that's why I accepted it.
0
 
hveldAuthor Commented:
There is one problem I didn't notice when tested
If I build a Sub from string and run it,
I can't set any variables, looks like they remain internal for the newly generated Sub, although I declared them as public in a module. Here is an example

in module1
-------------
Public a As String

in form1
--------------
Public Sub Command1_Click()

a = "Test NOT OK"

s = vbCrLf & "Public Sub test()" & vbCrLf & _
"a= ""TestOK""" & vbCrLf & _
"MsgBox a" & vbCrLf & _
"End sub" & vbCrLf

ScriptControl1.AddCode s
ScriptControl1.Run "test"

MsgBox a
End Sub

a retains a "Test NOT OK" value although it's set to "TestOK" in the generated Sub. Do you have an idea how to set variables from a sub created this way?

0
 
jklmnCommented:
Hi hveld,

You should get "TestOK" firstly and then get "Test NOT OK" later.
That is correct...because the code displays the value of a local variable 'a' inside Sub test() firstly....then displays the value of a public variable 'a' later...
What is wrong and what you need?
0
 
jklmnCommented:
Hi hveld,

Sorry....now I know what you meant....but got no idea how to do what you want....seems you need to use function to return a value.....
Any one got suggestions?
smozgur, any idea:-)
0
 
hveldAuthor Commented:
Yes, it works if I use a function, like this:

Public Sub Command1_Click()
Dim a As String, b As String
a = "Test NOT OK"

s = vbCrLf & "Public function test()" & vbCrLf & _
"a= ""TestOK""" & vbCrLf & _
"MsgBox a" & vbCrLf & _
"test = a" & vbCrLf & _
"End function" & vbCrLf

ScriptControl1.AddCode s
b = ScriptControl1.Eval("test")
MsgBox b
End Sub

But no way with sub's
BTW is it possible for someone to itercept commands I send to a script control, and the values returned?
For example can someone see the function test(),eventually the parameters it's called with, and what it returns?
Microsoft's script debugger can not, but is there some other possible way?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.