Shoutbox

Parameters by reference - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Parameters by reference (/showthread.php?tid=95793)

Parameters by reference by barby850717@gmail. on 11-05-2010 at 06:14 PM

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,


RE: Parameters by reference by Matti on 11-05-2010 at 07:29 PM

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. :)
RE: Parameters by reference by CookieRevised on 11-06-2010 at 12:12 AM

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.