Solved

VBScript function with ByRef parameters

Posted on 2002-05-23
12
1,721 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
Comment Utility
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
Comment Utility
Pankaj,
that's not the question.
The question is between Script Engines. Pls read the question properly
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:CJ_S
Comment Utility
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
Comment Utility
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
Comment Utility
Any update?
0
 
LVL 12

Expert Comment

by:ahosang
Comment Utility
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
Comment Utility
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
Comment Utility
As recommended

Chmod
Community Support Moderator @Experts Exchange
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now