3

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?

dakab
  • 5,379
  • 9
  • 43
  • 67
ClarkeyBoy
  • 4,934
  • 12
  • 49
  • 64

3 Answers3

3

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
cup
  • 7,589
  • 4
  • 19
  • 42
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.

sgeddes
  • 62,311
  • 6
  • 61
  • 83
  • Thanks for the reply, but the first solution didn't work unfortunately. For the 2nd solution, it takes less characters just to append it normally using `bob.name = bob.name & "andy"`; the entire point of this function is to reduce the number of characters by only referencing the variable, that I am appending to, once. – ClarkeyBoy Feb 09 '13 at 22:48
0

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.

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208