2

I've written an open source debug class for Classic ASP(VBScript) + firephp and want to know if it's possible to obtain the name of a variable that's been passed to a function? I've searched but can't seem to find any tricks to do this.

Example

Code:

    log myVariable

output:

    myVariable: "some value"

Any ideas?

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
David Meagor
  • 117
  • 2
  • 11

3 Answers3

4

I'm assuming the following code fragment describes your scenario:

Function myLog(myArg)
    ...
End Function

Dim xyz, abc
xyz = "some value"
abc = "some value"
myLog xyz
myLog abc

And you wish your function to know whether variables xyz or abc were used to call your function?

The answer is no for two reasons. One this type of argument calling is "call by value", and, even if it wasn't, there is still no mechanism inside vbscript for it know the original variable name you passed it.

Instead of variables have you thought of using the Dictionary Object:

<%
Dim cars 
Set cars = CreateObject("Scripting.Dictionary")
cars.Add "a", "Alvis" 
cars.Add "b", "Buick"
cars.Add "c", "Cadillac"
Response.Write "The value corresponding to the key 'b' is " & cars.Item("b") 
%>
Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
  • yea that's what I thought. I just wanted to make the logging function as quick and easy as possible. I can still do `log "myvalue",myvalue` it's just slightly more annoying to type ;) – David Meagor Jan 27 '12 at 16:37
  • Why the hell should I use Dim and then Set? Why not create and set values on the same declaration? Can that be possible? I am maintaining a really old ASP system and usually forget that "Dim" and errors explosions happen. – Francisco Souza Apr 06 '20 at 12:11
2

If you modify your input to the function a bit, you can achieve your result:

hello = "how do you do"
log "hello"

function log(x)
  response.write x & ":" & eval(x)
end function

This is using the eval function that will translate your string "hello" into the variable hello.

Lars Meldgård
  • 1,074
  • 10
  • 6
1

you can follow these steps

  1. create a new variable for each variable you want to print the name for each variable
  2. Save the desired variable name as a string in the relevant new variables
  3. encapsulate your code in a function that has two argument

Here is an example, I have used a Batch file called "move.bat" to run the VBS:

Dim sFoldervarName
sFoldervarName = "sfolder"
Dim sFolder
sFolder = "pippoplutopaperino"

call varnamefunc(sFolder, sFoldervarName)

Sub varnamefunc(arg1, arg2)
    LF = chr(10)
    Wscript.Echo("your variable name is:" + arg2 + LF + "your variable value is: " + arg1)
End Sub

here is the result:

c:\Batch_DEV>move.bat

your variable name is:sfolder
your variable value is: pippoplutopaperino

c:\Batch_DEV>

By doing this, your function returns the name of the variable and also the Variable value. Hope this helps! Regards.

Massyle Djama
  • 39
  • 1
  • 7