Calling a sub

D

dal.luc

Hello,

Here's a problem I can't solve.
Let n sub procedures A1, ... An

sub A1()
end sub
....
sub An()
end sub
....

I want to call indirectly one of them with a variable name like
this :

Ex :
myProc="A5"
Call myProc()
....

This doesn't work. Is there a way to solve this problem ?

Thanks for assistance !!
 
T

ThatsIT.net.au

Try somthing like this

allsubs("A1")

allsubs("An")

sub allsubs(job)
if job = "A1" then
...
else if job = "An"
...
end if
end sub
 
B

Bob Barrows [MVP]

dal.luc said:
Hello,

Here's a problem I can't solve.
Let n sub procedures A1, ... An

sub A1()
end sub
...
sub An()
end sub
...

I want to call indirectly one of them with a variable name like
this :

Ex :
myProc="A5"
Call myProc()
...

This doesn't work. Is there a way to solve this problem ?

Thanks for assistance !!
Use a Select Case statement or a long If ... ElesIf structure.

While I strongly discourage its use, Execute() can do this for you:

Execute(myProc)

For an explanation of why Execute() is discouraged see this article, which
is talking about Eval(), the jscript version of Execute():
http://blogs.msdn.com/ericlippert/archive/2003/11/04/53335.aspx. The points
made apply to Execute() as well.
 
D

Dave Anderson

dal.luc said:
Here's a problem I can't solve.
Let n sub procedures A1, ... An

sub A1()
end sub
...
sub An()
end sub
...

I want to call indirectly one of them with a variable name like
this :

Ex :
myProc="A5"
Call myProc()
...

This doesn't work. Is there a way to solve this problem ?

Yes. Among other things, you could use JScript as your ASP scripting
language:

var A = new Array()
A[1] = function() { // do stuff }
A[2] = function() { // do other stuff }
A["DescriptiveName"] = function() { // do something fun!! }

var myProc = "DescriptiveName"
A[myProc]()


(this is not a VBScript forum, after all)
 
D

Dave Anderson

Dave said:
This doesn't work. Is there a way to solve this problem ?

Yes. Among other things, you could use JScript as your ASP scripting
language:

var A = new Array()
A[1] = function() { // do stuff }
A[2] = function() { // do other stuff }
A["DescriptiveName"] = function() { // do something fun!! }

var myProc = "DescriptiveName"
A[myProc]()

I thought about this, and realized that this can probably be done with a
Scripting Dictionary in VBScript, which is a good analog for the JScript
array, as used in my example. Back to the specfics of your question:

Set D = CreateObject("Scripting.Dictionary")
For i = 1 To n
Call D.Add("A" & i, GetRef("A" & i))
Next
...
Call D(myProc)()
 
D

Dave Anderson

Bob said:
Great catch! I don't know why I never think of using GetRef ...

Because it didn't exist when you learned VBScript? Or maybe because VBScript
deliberately insulates us from the objects we use?

I certainly know why *I* didn't think of it right away -- I largely avoid
using VBScript. Strangely, when I decided to stick the object into the
dictionary, I immediately knew I needed GetRef, even though I had never used
it before.

JScript is just so much simpler to me...
 
D

dal.luc

Great catch! I don't know why I never think of using GetRef ...

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

Thanks, but it doesn't work !!!

In fact, I want to pass parameters in the proc and the proc is a
method provided by a dll.
It's like this : obj.Proc1(a,b,c)
The name "Proc1" is read on a database and the obj has been created
previously.

If you've any suggestions, thanks
Luc
 
A

Anthony Jones

Great catch! I don't know why I never think of using GetRef ...

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

Thanks, but it doesn't work !!!

In fact, I want to pass parameters in the proc and the proc is a
method provided by a dll.
It's like this : obj.Proc1(a,b,c)
The name "Proc1" is read on a database and the obj has been created
previously.

<<<<<

Passing parameters isn't so much a problem but calling a method on an object
like this in VBScript is.

I recommend you go back to Dave's original suggestion of using JScript.

For example this works in JScript:-


var o = new ActiveXObject("MSXML2.DOMDocument.3.0")
o.async = false

var sFile = "g:\\temp\\test.xml"
var sMethod = "load"

o[sMethod](sFile); // note method to call defined by a variable.

WScript.echo(o["xml"]) //properties work find also
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,100
Latest member
MelodeeFaj
Top