Classic ASP - passing a property as byref

你离开我真会死。 提交于 2020-11-29 09:24:26

问题


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?

Thanks in advance, Regards, Richard


回答1:


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



回答2:


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.




回答3:


As this other answer correctly states, you are facing limitation of the language itself.

The only other option to achieve what you are after as far as I can see it, is to add such sub routine to the class itself:

Public Sub Append(propName, strValue)
    Dim curValue, newValue
    curValue = Eval("Me." & propName)
    newValue = curValue & strValue
    Execute("Me." & propName & " = """ & Replace(newValue, """", """""") & """")
End Sub

Then to use it:

bob.Append "name", "andy"

Less elegant, but working.



来源:https://stackoverflow.com/questions/14790762/classic-asp-passing-a-property-as-byref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!