Link to home
Start Free TrialLog in
Avatar of FrenchJericho
FrenchJericho

asked on

VBScript function with ByRef parameters

Is there a way, in javascript, to pass a reference to a variable instead of the value itself?

For example,
<script language="vbscript">
Function Test(ByRef strRef)
   strRef= "Hello World"
End Function

Dim strVBTest
strVBTest = "Before"
Call Test(strVBTest)
MsgBox strVBTest
</script>

<script language="javascript">
var strJSTest = "Before";
Test(strJSTest);
alert(strJSTest);
</script>

The VB message box will display "Hello world" as expected but the JS alert will display "Before". How can I modify the javascript code so that it displays "Hello world"?
Avatar of Pankaj27
Pankaj27

FrenchJerico,

This can be done in two ways :

1)Instead of passing string, keep it in a array and pass the array. Do the operations on the array.

<script language="javascript">
var strJSTest = "Before";
var arTrial = new Array; //Create array
arTrial[0] = strJSTest //Populate the first element of array
Test(arTrial);
alert(arTrial[0]);

function Test(arTrial)
{
     arTrial[0] = "Hello World";
}
</script>

2) The Second one deals with creating an object with the string and passing it by reference. This is similar to passing any form field to the function.
Pankaj,
that's not the question.
The question is between Script Engines. Pls read the question properly
the explaination of strings suggests that what you are trying cannot be done.  However, I'm sure you could create an object and set a string property on that object (as in the article) or you can have the function return the string like this:

Function Test(ByRef strRef)
  strRef= "Hello World"
  Test = strRef
End Function

and then do this

strJSTest=Test(strJSTest);
Avatar of FrenchJericho

ASKER

knight,

one thing I did not mention is that modifying the function ('Test' in my simplistic example) is not an option to me. It's part of an imported .js file and make modifications to it would impact other parts of the application.
hmm, if you cannot modify the function at all, then even if I could offer a solution it would not be much help to you :)
Cannot be done without moving from one script to the other. The following does that. however, the javascript variable may NOT be declared or HAS to be global. otherwise it won't work.

<script language="vbscript">
Function Test(byref strRef)
  strRef= "Hello World"
End Function

Dim strVBTest
strVBTest = "Before"
Call Test(strVBTest)
MsgBox strVBTest

Dim mParameters()
Function varptr(byval sR)
     Dim sTmp, mArr
     sFunction = Left(sR, InStr(sR, "("))
     sTmp = Mid(sR, InStr(sR, "(")+1, Len(sR)-(InStr(sR, "(")+1))
     mArr = Split(sTmp, ",")
     redim mParameters(UBound(mArr))

     Dim sCall
     For i=LBound(mParameters) To UBound(mParameters)
          sCall = sCall & "mParameters(" & i & "), "
     Next
     sCall = Left(sCall, Len(sCall)-2)
     Eval(sFunction & sCall & ")")

     For i=LBound(mArr) To UBound(mArr)
          VBEval(mArr(i) & " = """ & mParameters(i) & """")
     Next
End Function
</script>

<script language="javascript">
function VBEval(sStr){eval(sStr)}

strJSTest = "Before";
varptr("Test(strJSTest)");
alert(strJSTest);
</script>

CJ
as you can see you only need an additional functioncall within your javascript otherwise you cannot do it.

CJ
Any update?
This question has been abandoned. I will make a recommendation to the moderators on its resolution in a week or so. I appreciate any comments that would help me to make a recommendation.
 
In the absence of responses, I may recommend DELETE unless it is clear to me that it has value as a PAQ. Silence = you don't care
 
ahosang
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ - No Refund
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
ahosang
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of Chmod
Chmod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial