Solved

VBScript function with ByRef parameters

Posted on 2002-05-23
12
1,736 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

707 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