Solved

VBScript function with ByRef parameters

Posted on 2002-05-23
12
1,725 Views
Last Modified: 2007-11-27
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
Comment
Question by:FrenchJericho
  • 3
  • 3
  • 2
  • +4
12 Comments
 
LVL 1

Expert Comment

by:Pankaj27
ID: 7031060
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 7031775
Pankaj,
that's not the question.
The question is between Script Engines. Pls read the question properly
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7032127
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 33

Expert Comment

by:knightEknight
ID: 7032129
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
 

Author Comment

by:FrenchJericho
ID: 7032137
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 7032149
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
 
LVL 22

Expert Comment

by:CJ_S
ID: 7034827
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
 
LVL 22

Expert Comment

by:CJ_S
ID: 7034828
as you can see you only need an additional functioncall within your javascript otherwise you cannot do it.

CJ
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7048368
Any update?
0
 
LVL 12

Expert Comment

by:ahosang
ID: 8263041
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
 
LVL 12

Expert Comment

by:ahosang
ID: 8356699
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
 

Accepted Solution

by:
Chmod earned 0 total points
ID: 8404921
As recommended

Chmod
Community Support Moderator @Experts Exchange
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question