hveld
asked on
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 :)
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 :)
Ooops, I meant
Print ScriptControl1.Eval("2 + 3")
Print ScriptControl1.Eval("2 + 3")
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
Then you can create runtime sub/functions and also use that great Eval method like jklmn said.
suat
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....
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....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, that should be
ScriptControl1.Run MyStr
ScriptControl1.Run MyStr
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
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
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
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
I am sorry. Wrong window :)
suat
suat
No no, right window :) twice.
suat
suat
Oh, similar but jklmn is fastest.
Congratulations
suat
Congratulations
suat
Wow ! Didn't think of that!
Way to go jklmn and suat (or is it sure now :>)!!
D'Mzzl!
RoverM
Way to go jklmn and suat (or is it sure now :>)!!
D'Mzzl!
RoverM
"sure" was for ", can you answer that? I am listening...." of jklm.
:)
suat
:)
suat
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?
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?
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
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
If S1, S2 and S3 are public methods of an object (a form for example), you can use CallByName to call them.
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
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
The best answers can be soooooooo easy....:>
Way to go emoreau!
D'Mzzl!
RoverM
Way to go emoreau!
D'Mzzl!
RoverM
I agree...
suat
suat
Hi roverm
D'Mzzl!
is EE dan vergeven van de hollanders ?
geintje :-)
en natuurlijk van een belg
D'Mzzl!
is EE dan vergeven van de hollanders ?
geintje :-)
en natuurlijk van een belg
Ik denk het ! ;-)
Pierre: You're the second to understand my signature!
D'Mzzl!
RoverM
Pierre: You're the second to understand my signature!
D'Mzzl!
RoverM
ASKER
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.
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.
ASKER
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?
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?
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?
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?
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:-)
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:-)
ASKER
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?
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?
in VB you can do
Print ScriptControl1.Eval(2 + 3)
Is this what you want in you 'BTW'?