if I have a script with two functions named the same.. such as

msgbox testme(8,5)

function testme (int1,int2)
testme = int1 + int2
end function

function testme (int1,int2)
testme = int1 - int2
end function

The output is the result of the last function in line in the script ie 3.
However if you add the line

executeglobal "function testme(int1,int2) : testme = int1 * int2 : end
function"

above the first function, then the result is 40.. ie it runs the first
function in the script?

adding a second executeglobal line after the first..

executeglobal "function testme(int1,int2) : testme = int1 / int2 : end
function"

and the second executeglobal function is run ie the output is 1.6. isnt that
a little bit strange? how does the interpretation of the script change using
executeglobal I wonder ? I assume that the executeglobal command is actually
run after the rest of the code so the function is in fact the last version of
the function?

If that is the case why does this code

executeglobal "msgbox testme (24,1)"

msgbox testme(8,5)

function testme (int1,int2)
testme = int1 - int2
end function

give the output 23 then 3 rather than the other way around?

Re: functions named the same by Paul

Paul
Mon Dec 03 16:53:13 PST 2007


"Robert" <Robert@discussions.microsoft.com> wrote in message
news:026383B3-EA1E-4975-AB13-85223CC012D7@microsoft.com...
> if I have a script with two functions named the same.. such as
>
> msgbox testme(8,5)
>
> function testme (int1,int2)
> testme = int1 + int2
> end function
>
> function testme (int1,int2)
> testme = int1 - int2
> end function
>
> The output is the result of the last function in line in the script ie 3.
> However if you add the line
>
> executeglobal "function testme(int1,int2) : testme = int1 * int2 : end
> function"
>
> above the first function, then the result is 40.. ie it runs the first
> function in the script?
>
> adding a second executeglobal line after the first..
>
> executeglobal "function testme(int1,int2) : testme = int1 / int2 : end
> function"
>
> and the second executeglobal function is run ie the output is 1.6. isnt
> that
> a little bit strange? how does the interpretation of the script change
> using
> executeglobal I wonder ? I assume that the executeglobal command is
> actually
> run after the rest of the code so the function is in fact the last version
> of
> the function?
>
> If that is the case why does this code
>
> executeglobal "msgbox testme (24,1)"
>
> msgbox testme(8,5)
>
> function testme (int1,int2)
> testme = int1 - int2
> end function
>
> give the output 23 then 3 rather than the other way around?

This is just a wild guess.
Prior to executing the script, it is sort of precompiled. At this time, the
inline functions and subroutines are defined, so that the first statement of
the script can access the function even if it is way at the end of the
script. If there are duplicate names, the last definition is the one that
will be used, unless something overrides it.

Then the code is executed. Every time an execute statement redefines the
function,
like this:
> executeglobal "function testme(int1,int2) : testme = int1 / int2 : end
> function"
then that definition will be used until it is redefined again.

-Paul Randall



Re: functions named the same by Robert

Robert
Mon Dec 03 22:01:01 PST 2007

I see what you mean and that sounds very plausible... offers an intriguing
possibility as well... I am sure I can get better than this but you can "kind
of" override a function like this....

on error resume next

Dim stringtorun
stringtorun = "msgbox testme(8,5,4)"

executeglobal stringtorun

if err.number <> 0 then
executeglobal "function testme(int1,int2,int3) : testme = int1 + int2 +
int3: end function"
executeglobal stringtorun
End if

on error goto 0

function testme (int1,int2)
testme = int1 - int2
end function

In this code the function with two parameters is called but if an error is
generated (wrong number of parameters) then the same code is called again,
this time it works taking the function definition added using the execute
global command,,,

I wonder if I could get some kind of generic override function to work? I
will have a go...

Robert



"Paul Randall" wrote:


>
> This is just a wild guess.
> Prior to executing the script, it is sort of precompiled. At this time, the
> inline functions and subroutines are defined, so that the first statement of
> the script can access the function even if it is way at the end of the
> script. If there are duplicate names, the last definition is the one that
> will be used, unless something overrides it.
>
> Then the code is executed. Every time an execute statement redefines the
> function,
> like this:
> > executeglobal "function testme(int1,int2) : testme = int1 / int2 : end
> > function"
> then that definition will be used until it is redefined again.
>
> -Paul Randall
>
>
>

Re: functions named the same by Anthony

Anthony
Tue Dec 04 01:17:52 PST 2007

"Paul Randall" <paulr90@aol.com> wrote in message
news:e%23FCRAhNIHA.2064@TK2MSFTNGP06.phx.gbl...
>
> "Robert" <Robert@discussions.microsoft.com> wrote in message
> news:026383B3-EA1E-4975-AB13-85223CC012D7@microsoft.com...
> > if I have a script with two functions named the same.. such as
> >
> > msgbox testme(8,5)
> >
> > function testme (int1,int2)
> > testme = int1 + int2
> > end function
> >
> > function testme (int1,int2)
> > testme = int1 - int2
> > end function
> >
> > The output is the result of the last function in line in the script ie
3.
> > However if you add the line
> >
> > executeglobal "function testme(int1,int2) : testme = int1 * int2 : end
> > function"
> >
> > above the first function, then the result is 40.. ie it runs the first
> > function in the script?
> >
> > adding a second executeglobal line after the first..
> >
> > executeglobal "function testme(int1,int2) : testme = int1 / int2 : end
> > function"
> >
> > and the second executeglobal function is run ie the output is 1.6. isnt
> > that
> > a little bit strange? how does the interpretation of the script change
> > using
> > executeglobal I wonder ? I assume that the executeglobal command is
> > actually
> > run after the rest of the code so the function is in fact the last
version
> > of
> > the function?
> >
> > If that is the case why does this code
> >
> > executeglobal "msgbox testme (24,1)"
> >
> > msgbox testme(8,5)
> >
> > function testme (int1,int2)
> > testme = int1 - int2
> > end function
> >
> > give the output 23 then 3 rather than the other way around?
>
> This is just a wild guess.
> Prior to executing the script, it is sort of precompiled. At this time,
the
> inline functions and subroutines are defined, so that the first statement
of
> the script can access the function even if it is way at the end of the
> script. If there are duplicate names, the last definition is the one that
> will be used, unless something overrides it.
>
> Then the code is executed. Every time an execute statement redefines the
> function,
> like this:
> > executeglobal "function testme(int1,int2) : testme = int1 / int2 : end
> > function"
> then that definition will be used until it is redefined again.
>

Thats exactly what it does, pretty good for a "wild guess" ;)

--
Anthony Jones - MVP ASP/ASP.NET



Re: functions named the same by Anthony

Anthony
Tue Dec 04 01:49:53 PST 2007

"Robert" <Robert@discussions.microsoft.com> wrote in message
news:A7424D35-7C6F-45E2-BFB1-5897A56EA14F@microsoft.com...
> I see what you mean and that sounds very plausible... offers an intriguing
> possibility as well... I am sure I can get better than this but you can
"kind
> of" override a function like this....
>
> on error resume next
>
> Dim stringtorun
> stringtorun = "msgbox testme(8,5,4)"
>
> executeglobal stringtorun
>
> if err.number <> 0 then
> executeglobal "function testme(int1,int2,int3) : testme = int1 + int2
+
> int3: end function"
> executeglobal stringtorun
> End if
>
> on error goto 0
>
> function testme (int1,int2)
> testme = int1 - int2
> end function
>
> In this code the function with two parameters is called but if an error is
> generated (wrong number of parameters) then the same code is called again,
> this time it works taking the function definition added using the execute
> global command,,,
>
> I wonder if I could get some kind of generic override function to work? I
> will have a go...
>


You could get some form of function override going but what your code
described is overloading not overriding.

This far from a good solution to providing overloads Any such solution
would have to rely on errors being thrown (which means you'll have resume
next in effect therefore hiding other errors that might occur in your code),
using executeglobal liberally to redefine an re-redefine the same functions
all over the place and all of this would have to occur inline with the rest
of the code since it can't be placed in a function (else what would be the
point).

A compiler that handles overloads detects the version of a function required
by the number of parameters and the types of those parameters. Since from
the VBScript syntax point of view there is only one type (Variant) then the
only thing that could vary from one overload to the next is the number of
parameters.

Since the number of parameters is can be seen when making a call to function
then the following would be way easier:-

MsgBox TestMe2(int1, int2)
MsgBox TestMe3(int1, int2, int3)

Ok suffixing the function name with the number of parameters isn't as
elegant as when you have a compilier that supports overloads but its a
million times better than using the executeglobal marlarky.

Internally a function can use TypeName, VarType and the IsXxxx functions to
determine the types of its parameters and vary its response accordingly.


As to actually overriding, well mostly overriding is something primarily
useful in a OOD that includes both base and derived classes. Since VBScript
doesn't support inheritance this level of OOD would be very difficult to
acheive for all sorts of reasons.

The idea of being able to override a specific function with another is a
reasonable and doable one. However ExecuteGlobal isn't the answer.

Take a look at this:-

Dim TestMe

Function TestMeAdd(int1, int2)
TestMeAdd = int1 + int2
End Function

Function TestMeSubtract(int1, int2)
TestMeSubtract = int1 - int2
End Function

Set TestMe = GetRef("TestMeAdd")

MsgBox TestMe(8, 5)

Set TestMe = GetRef("TestMeSubtract")

MsgBox TestMe(8, 5)


GetRef returns an object that acts as a reference to a named function. The
get function of the default property of the reference object delegates to
the function being referenced and returns that functions return value.
Hence the variable once set can be used in code as if it were the referenced
function. However since its actually an variable holding a reference to an
object it can be reassigned at any point. All this with out ExecuteGlobals
invoking compiliers and redefining code which would be real slow an quite
ugly.

--
Anthony Jones - MVP ASP/ASP.NET



Re: functions named the same by ekkehard

ekkehard
Tue Dec 04 02:25:16 PST 2007

Robert schrieb:
> I see what you mean and that sounds very plausible... offers an intriguing
> possibility as well... I am sure I can get better than this but you can "kind
> of" override a function like this....
>
> on error resume next
>
> Dim stringtorun
> stringtorun = "msgbox testme(8,5,4)"
>
> executeglobal stringtorun
>
> if err.number <> 0 then
> executeglobal "function testme(int1,int2,int3) : testme = int1 + int2 +
> int3: end function"
> executeglobal stringtorun
> End if
>
> on error goto 0
>
> function testme (int1,int2)
> testme = int1 - int2
> end function
>
> In this code the function with two parameters is called but if an error is
> generated (wrong number of parameters) then the same code is called again,
> this time it works taking the function definition added using the execute
> global command,,,
>
> I wonder if I could get some kind of generic override function to work? I
> will have a go...
[...]
If by "intriguing" you mean "luring you into dangerous territory" I'd
agree with you. Looking at the source code should make it easy to predict
reliably what the program will do given certain circumstances/inputs.
Using error traping to change the script on the fly will increase the
risk of surprises that will be nasty most of the time.
"Overriding functions" in the sense of "having different/non related functions
with the same name" can be done in VBScript by using different classes; e.g.:

Class D2Shape
...
Sub moveTo( iX, iY )
...

Class D3Shape
...
Sub moveTo( iX, iY, iZ )

"Overriding functions" in the sense of "redefining a function of a superclass
in a subclass" can't be done in VBScript because the lack of inheritance.
If I'd need two functions testme( i1, i2 ) and testme( i1, i2, i3 ) then I'd
call them in an If or Select depending on an explicitly handled condition:

If IsD3( vVar ) Then
testme i1, i2, i3
Else
testme i1, i2
End If

That way I can be sure that the script will work when this code is called for
Not IsD3() after it was called for IsD3() - for such a condition your solution
would give you a very nasty surprise.



Re: functions named the same by Alexander

Alexander
Tue Dec 04 15:09:04 PST 2007

Robert schrieb:

> I see what you mean and that sounds very plausible... offers an intriguing
> possibility as well... I am sure I can get better than this but you can "kind
> of" override a function like this....
>
> on error resume next
>
> Dim stringtorun
> stringtorun = "msgbox testme(8,5,4)"
>
> executeglobal stringtorun
>
> if err.number <> 0 then
> executeglobal "function testme(int1,int2,int3) : testme = int1 + int2 +
> int3: end function"
> executeglobal stringtorun
> End if
>
> on error goto 0
>
> function testme (int1,int2)
> testme = int1 - int2
> end function
>
> In this code the function with two parameters is called but if an error is
> generated (wrong number of parameters) then the same code is called again,
> this time it works taking the function definition added using the execute
> global command,,,
>
> I wonder if I could get some kind of generic override function to work? I
> will have a go...

Use JScript and pass in any number of arguments you like.

function addIt()
{
var a = arguments, i = a.length, r = 0;
while (i--)
r += isNaN(a[i]) ? 0 : a[i];
return r;
}

WSH.Echo(addIt(1,2,3,4,5) * addIt(1,2));


Also JScript provides real means for dynamically creating new
function definitons on runtime with its Function-object:

var divide = new Function ("dividend", "divisor",
"return (divisor == 0 ? null : dividend/divisor);"
);

WSH.Echo (divide(10,3));


and also for removing the function:

divide = undefined;


So you can also temporarily override it:

function math() {
var a = arguments, i = a.length, r = 0;
while (i--) r += isNaN(a[i]) ? 0 : a[i];
return r;
}

WSH.Echo(math(1,2,3,4,5));
var mathOld = math;

math = new Function(
"var a = arguments, i = a.length, r = 1;"
+ "while (i--) r *= isNaN(a[i]) ? 1 : a[i];return r;"
);


WSH.Echo(math(1,2,3,4,5));
math = mathOld;

WSH.Echo(math (1,2,3,4,5));


MfG,
Alex




Re: functions named the same by Anthony

Anthony
Wed Dec 05 02:13:01 PST 2007

"Alexander Mueller" <millerax@hotmail.com> wrote in message
news:4755de14$0$17525$9b4e6d93@newsspool4.arcor-online.net...
> Robert schrieb:
>
> > I see what you mean and that sounds very plausible... offers an
intriguing
> > possibility as well... I am sure I can get better than this but you can
"kind
> > of" override a function like this....
> >
> > on error resume next
> >
> > Dim stringtorun
> > stringtorun = "msgbox testme(8,5,4)"
> >
> > executeglobal stringtorun
> >
> > if err.number <> 0 then
> > executeglobal "function testme(int1,int2,int3) : testme = int1 +
int2 +
> > int3: end function"
> > executeglobal stringtorun
> > End if
> >
> > on error goto 0
> >
> > function testme (int1,int2)
> > testme = int1 - int2
> > end function
> >
> > In this code the function with two parameters is called but if an error
is
> > generated (wrong number of parameters) then the same code is called
again,
> > this time it works taking the function definition added using the
execute
> > global command,,,
> >
> > I wonder if I could get some kind of generic override function to work?
I
> > will have a go...
>
> Use JScript and pass in any number of arguments you like.
>
> function addIt()
> {
> var a = arguments, i = a.length, r = 0;
> while (i--)
> r += isNaN(a[i]) ? 0 : a[i];
> return r;
> }
>
> WSH.Echo(addIt(1,2,3,4,5) * addIt(1,2));
>
>
> Also JScript provides real means for dynamically creating new
> function definitons on runtime with its Function-object:
>
> var divide = new Function ("dividend", "divisor",
> "return (divisor == 0 ? null : dividend/divisor);"
> );
>
> WSH.Echo (divide(10,3));
>
>
> and also for removing the function:
>
> divide = undefined;
>
>
> So you can also temporarily override it:
>
> function math() {
> var a = arguments, i = a.length, r = 0;
> while (i--) r += isNaN(a[i]) ? 0 : a[i];
> return r;
> }
>
> WSH.Echo(math(1,2,3,4,5));
> var mathOld = math;
>
> math = new Function(
> "var a = arguments, i = a.length, r = 1;"
> + "while (i--) r *= isNaN(a[i]) ? 1 : a[i];return r;"
> );
>
>
> WSH.Echo(math(1,2,3,4,5));
> math = mathOld;
>
> WSH.Echo(math (1,2,3,4,5));
>

Note though if the code for the function is known at design time then its
better to avoid instancing a function with a string. Instead use:-


var divide = function (dividend, divisor) {
return (divisor == 0 ? null : dividend/divisor);
}

OR

math = function() {
var a = arguments, i = a.length, r = 1;
while (i--) r *= isNaN(a[i]) ? 1 : a[i];return r;
}


That way the parser can flag up syntax errors when the file is loaded
instead of waiting until the function gets instanced. It will also be
quicker since the parser need only process the function body once.

I think its a design flaw to define a named function then override it with a
different implementation. It would be better to use a variable to start
with so that it is clear that the actual function that gets executed may
vary.




--
Anthony Jones - MVP ASP/ASP.NET