Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Calling a sub

Reply
Thread Tools

Calling a sub

 
 
dal.luc
Guest
Posts: n/a
 
      01-09-2008
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 !!
 
Reply With Quote
 
 
 
 
ThatsIT.net.au
Guest
Posts: n/a
 
      01-09-2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 !!


 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      01-09-2008
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/ar.../04/53335.aspx. 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"


 
Reply With Quote
 
Dave Anderson
Guest
Posts: n/a
 
      01-09-2008
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.


 
Reply With Quote
 
Dave Anderson
Guest
Posts: n/a
 
      01-09-2008
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.


 
Reply With Quote
 
Dave Anderson
Guest
Posts: n/a
 
      01-09-2008
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/lib...10(VS.85).aspx


--
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.


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      01-09-2008
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/lib...10(VS.85).aspx
>
>

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.


 
Reply With Quote
 
Dave Anderson
Guest
Posts: n/a
 
      01-09-2008
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.


 
Reply With Quote
 
dal.luc
Guest
Posts: n/a
 
      01-11-2008
On 9 jan, 21:50, "Bob Barrows [MVP]" <(E-Mail Removed)> 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/lib...10(VS.85).aspx

>
> 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
 
Reply With Quote
 
Anthony Jones
Guest
Posts: n/a
 
      01-11-2008
"dal.luc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On 9 jan, 21:50, "Bob Barrows [MVP]" <(E-Mail Removed)> 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/lib...10(VS.85).aspx

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Death To Sub-Sub-Sub-Directories! Lawrence D'Oliveiro Java 92 05-20-2011 06:50 AM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
Question about sub calling other sub Martin Kissner Perl Misc 8 03-09-2006 06:21 PM
Legitimate use of calling a sub as &sub Richard Gration Perl Misc 1 02-23-2005 05:43 PM
how do make a pop-up in sub ASP.net sub ? THY ASP .Net 1 08-18-2003 11:30 PM



Advertisments