• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1751
  • Last Modified:

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"?
0
FrenchJericho
Asked:
FrenchJericho
  • 3
  • 3
  • 2
  • +4
1 Solution
 
Pankaj27Commented:
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.
0
 
b1xml2Commented:
Pankaj,
that's not the question.
The question is between Script Engines. Pls read the question properly
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
knightEknightCommented:
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);
0
 
FrenchJerichoAuthor Commented:
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.
0
 
knightEknightCommented:
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 :)
0
 
CJ_SCommented:
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
0
 
CJ_SCommented:
as you can see you only need an additional functioncall within your javascript otherwise you cannot do it.

CJ
0
 
CJ_SCommented:
Any update?
0
 
ahosangCommented:
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
0
 
ahosangCommented:
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
0
 
ChmodCommented:
As recommended

Chmod
Community Support Moderator @Experts Exchange
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now