Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classic ASP - passing a property as byref

In Classic ASP, I have an object, call it bob. This then has a property called name, with let and get methods.

I have a function as follows:

sub append(byref a, b)
    a = a & b
end sub

This is simply to make it quicker to add text to a variable. I also have the same for prepend, just it is a = b & a. I know it would be simple to say bob.name = bob.name & "andy", but I tried using the above functions and neither of them work.

The way I am calling it is append bob.name, "andy". Can anyone see what is wrong with this?

like image 370
ClarkeyBoy Avatar asked Sep 15 '25 00:09

ClarkeyBoy


2 Answers

Unfortunately this is a feature of VBScript. It is documented in http://msdn.microsoft.com/en-us/library/ee478101(v=vs.84).aspx under "Argument in a class". The alternative is to use a function. Here is an example illustrating the difference. You can run this from the command line using "cscript filename.vbs.

sub append (a, b)
   a = a & b
end sub

function Appendix(a, b)
   Appendix = a & b
end function

class ClsAA
   dim m_b
   dim m_a
end class
dim x(20)

a = "alpha"
b = "beta"
wscript.echo "variable works in both cases"
append a, b
wscript.echo "sub " & a
a = appendix(a, b)
wscript.echo "function " & a

x(10) = "delta"
wscript.echo "array works in both cases"
append x(10), b
wscript.echo "sub " & x(10)
x(10) = appendix( x(10), b)
wscript.echo "function " & x(10)

set objAA = new ClsAA
objAA.m_a = "gamma"
wscript.echo "Member only works in a function"
append objAA.m_a, b
wscript.echo "sub " & objAA.m_a
objAA.m_a = appendix(objAA.m_a, b)
wscript.echo "function " & objAA.m_a
like image 72
cup Avatar answered Sep 17 '25 20:09

cup


Have you tried using with the keyword CALL:

call append (bob.name, "andy")

Classic ASP is fickel about ByRef and ByVal. By default it uses ByRef -- no reason to specify that. If you call a function with parenthesis (without the call), it will pass the variables as ByVal.

Alternatively, you could accomplish the same with:

function append(byref a, b)
    append = a & b
end sub

bob.name = append(bob.name, "andy");

Good luck.

like image 27
sgeddes Avatar answered Sep 17 '25 20:09

sgeddes