【问题标题】:Classic ASP - passing a property as byref经典 ASP - 将属性作为 byref 传递
【发布时间】:2013-01-25 07:01:34
【问题描述】:

在经典 ASP 中,我有一个对象,称之为 bob。然后它有一个名为name 的属性,带有letget 方法。

我有一个功能如下:

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

这只是为了更快地将文本添加到变量中。我对prepend 也有同样的看法,只是a = b & a。我知道说bob.name = bob.name & "andy" 很简单,但我尝试使用上述函数,但它们都不起作用。

我称呼它的方式是append bob.name, "andy"。谁能看出这有什么问题?

【问题讨论】:

  • 我也在bob 中尝试过这个,使用append Title, "andy",但它仍然没有工作。 :(

标签: properties asp-classic pass-by-reference byref


【解决方案1】:

不幸的是,这是 VBScript 的一个特性。它记录在“类中的参数”下的http://msdn.microsoft.com/en-us/library/ee478101(v=vs.84).aspx 中。另一种方法是使用函数。这是一个说明差异的示例。您可以使用“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】:

    您是否尝试过使用关键字CALL

    call append (bob.name, "andy")
    

    经典的 ASP 对 ByRef 和 ByVal 的看法是反复无常的。默认情况下,它使用 ByRef - 没有理由指定它。如果你调用一个带括号的函数(没有调用),它会将变量作为 ByVal 传递。

    或者,您可以通过以下方式完成相同的操作:

    function append(byref a, b)
        append = a & b
    end sub
    
    bob.name = append(bob.name, "andy");
    

    祝你好运。

    【讨论】:

    • 感谢您的回复,但不幸的是第一个解决方案没有奏效。对于第二种解决方案,只需使用bob.name = bob.name & "andy" 正常附加它就需要更少的字符;这个函数的重点是通过仅引用我要附加的变量来减少字符数。
    【解决方案3】:

    正如this other answer 正确指出的那样,您正面临语言本身的限制。

    据我所知,实现您所追求的唯一其他选择是将此类子例程添加到类本身:

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

    然后使用它:

    bob.Append "name", "andy"
    

    不太优雅,但很有效。

    【讨论】:

      猜你喜欢
      • 2010-11-01
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多