Calling a sub

Calling a sub

am 09.01.2008 09:05:55 von 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 !!

Re: Calling a sub

am 09.01.2008 09:55:15 von me

Try somthing like this

allsubs("A1")

allsubs("An")

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



"dal.luc" wrote in message
news:a096403b-57e1-49c8-9667-11bb7811c5a5@q77g2000hsh.google groups.com...
> 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 !!

Re: Calling a sub

am 09.01.2008 13:01:19 von reb01501

dal.luc wrote:
> 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.a spx. The points
made apply to Execute() as well.
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Calling a sub

am 09.01.2008 18:14:55 von Dave Anderson

dal.luc wrote:
> 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)


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.

Re: Calling a sub

am 09.01.2008 18:43:55 von Dave Anderson

Dave Anderson wrote:

>> 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)()


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.

Re: Calling a sub

am 09.01.2008 19:04:56 von Dave Anderson

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

Silly me. You can skip the dictionary and use GetRef() directly:

Call GetRef(myProc)()

This does not appear to be as fraught with peril as using Execute() or
Eval().

http://msdn2.microsoft.com/en-us/library/ekabbe10(VS.85).asp x


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.

Re: Calling a sub

am 09.01.2008 21:50:55 von reb01501

Dave Anderson wrote:
> Dave Anderson wrote:
>> Set D = CreateObject("Scripting.Dictionary")
>> For i = 1 To n
>> Call D.Add("A" & i, GetRef("A" & i))
>> Next
>> ...
>> Call D(myProc)()
>
> Silly me. You can skip the dictionary and use GetRef() directly:
>
> Call GetRef(myProc)()
>
> This does not appear to be as fraught with peril as using Execute() or
> Eval().
>
> http://msdn2.microsoft.com/en-us/library/ekabbe10(VS.85).asp x
>
>
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.

Re: Calling a sub

am 09.01.2008 22:48:09 von Dave Anderson

Bob Barrows [MVP] wrote:
> 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...



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.

Re: Calling a sub

am 11.01.2008 14:54:49 von dal.luc

On 9 jan, 21:50, "Bob Barrows [MVP]" wrote:
> Dave Anderson wrote:
> > Dave Anderson wrote:
> >> =A0 =A0Set D =3D CreateObject("Scripting.Dictionary")
> >> =A0 =A0For i =3D 1 To n
> >> =A0 =A0 =A0 =A0Call D.Add("A" & i, GetRef("A" & i))
> >> =A0 =A0Next
> >> =A0 =A0...
> >> =A0 =A0Call D(myProc)()
>
> > Silly me. You can skip the dictionary and use GetRef() directly:
>
> > =A0 =A0 Call GetRef(myProc)()
>
> > This does not appear to be as fraught with peril as using Execute() or
> > Eval().
>
> >http://msdn2.microsoft.com/en-us/library/ekabbe10(VS.85).as px
>
> 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 messag=
es pr=E9c=E9dents -
>
> - Afficher le texte des messages pr=E9c=E9dents -

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

Re: Calling a sub

am 11.01.2008 15:39:50 von Anthony Jones

"dal.luc" wrote in message
news:cbaf829e-efbe-4638-9420-d709ae628bdd@e4g2000hsg.googleg roups.com...
On 9 jan, 21:50, "Bob Barrows [MVP]" wrote:
>>>>>
> Dave Anderson wrote:
> > Dave Anderson wrote:
> >> Set D = CreateObject("Scripting.Dictionary")
> >> For i = 1 To n
> >> Call D.Add("A" & i, GetRef("A" & i))
> >> Next
> >> ...
> >> Call D(myProc)()
>
> > Silly me. You can skip the dictionary and use GetRef() directly:
>
> > Call GetRef(myProc)()
>
> > This does not appear to be as fraught with peril as using Execute() or
> > Eval().
>
> >http://msdn2.microsoft.com/en-us/library/ekabbe10(VS.85).as px
>
> 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





--
Anthony Jones - MVP ASP/ASP.NET