What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Parameters by reference

Parameters by reference
Author: Message:
barby850717@gmail.
New Member
*


Posts: 9
Joined: Oct 2010
O.P. Parameters by reference
Hi, I was wondering if there is any way I can pass parameters by reference.

I mean, I have a method A that passes a string as a parameter for the method B. I need to modify the string inside the method B and have the modified string when I return to method B.

My method B is inside a DLL I am using, therefore it already receives the string as a reference and modifies it. However, when the execution gets back to the method A, the string has not changed.

Please help!!

Thanks in advance,
11-05-2010 06:14 PM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Parameters by reference
There's no such thing as passing arguments by reference in JScript. Instead, depending on the variable type, variables are changed or recreated. This is what we call "immutable" types.

For example, the string type is immutable: you can't change its value after you create one. Instead, all operations you do on the string actually create a new string:
js code:
var string = "Hello";
string += " world";
In this example, by appending "world" to string, you create a new string "Hello world" instead of changing it. The effect isn't very noticeable here, but it is when you use the value in more than one place.
js code:
var obj = {
    foo: "bar"
};
var string = obj.foo; // string holds "bar"
string += "tender"; // string is now a new string "bartender"
// obj.prop hasn't changed and is still "bar"
Here, an object with a string property is defined and then the property value is assigned to a variable. When an operation is performed on this variable, a new string is created which means that the object property stays intact. Therefore you don't have to worry about the object properties accidentally being changed because you're changing a variable holding a property.

The basic value types in JScript are immutable: number, string, boolean,... However objects an functions are pointer types, meaning they are always passed by reference.
js code:
var obj = {
  foo: "bar"
};
var newobj = obj; // newobj now also points to obj
newobj.foo = "bartender";
//obj.foo has also changed to "bartender"
Instead of copying the data from obj to a new object, newobj just points to the same data as obj. Therefore, changing a property on newobj also changed obj since they're exactly the same.

Now, a possible solution to your problem would be to pass the string argument as a property of a containing object and let your DLL change the property on that object instead. When your DLL function finishes, the data in the JScript object should be changed by the DLL, allowing you to read the new string value from the object. Your script would look something like this:
js code:
var obj = {
   foo: "bar"
};
var x = new ActiveXObject("MyScript.MyClass");
x.DoIt( obj );
// obj.foo nows hold the changed value
I don't know how you implemented your DLL interactions or how a JScript object is received in the DLL function - I haven't used DLLs a lot in my own scripts. I believe this should get you started. :)
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
11-05-2010 07:29 PM
Profile E-Mail PM Web Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Parameters by reference
Or use COM and call the DLL procedure with a pointer to an allocated datablock using the Interop functions in Plus!....

It all depends on what kind of DLL you're talking about and how that 'method B' is programmed.
.-= A 'frrrrrrrituurrr' for Wacky =-.
11-06-2010 12:12 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On