Message-ID: <585651453.1561.1432786563989.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_1560_358438584.1432786563989" ------=_Part_1560_358438584.1432786563989 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Java and Groovy are bot= h "pass-by-value" languages. That is, if you pass a value t= o a method and the method modifies it, you don't see a change in the callin= g scope.
COM, on the other hand, supports both "pass-by-val= ue" (or "byval") and "pass-by-reference" (or "= ;byref"). "Byval" is sometime referred to as an "= in" parameter, and "byref" is sometimes referred to as an &q= uot;in/out" parameter, reflecting the direction the data is flowing.&n= bsp; When you call a method that accepts a "pass-by-reference" pa= rameter, the method can modify the parameter value, and this will be reflec= ted in the calling scope.=20
Byref argument passing was always a favorite of C/C++ p= rogrammers, who often use the result of a method to pass exception informat= ion. It was the default way to pass values in versions of Vis= ual Basic through 6. With the advent of mod= ern try/catch exception handling, modifying values inside a metho= d is generally considered bad programming practice. Visual Basic (and COM) still supports byref, but= byval is now defined as the default setting. You should not have to = deal with byref parameters often, especially when dealing with APIs that ar= e designed for scripting. That doesn't mean it will never happen, thou= gh.=20
It shouldn't surprise you that Scriptom supports p= assing parameters by-reference, just in case you need it. ==20
There are actually two ways that values are passed by-reference.&nb= sp; For COM Events, which d= o not support returning values, the only way to pass back information = is through a byref parameter. That is all explained in the arti= cle, and it is actually pretty transparent. You don't have to do anyt= hing special for it to work. Your event handler can just change one o= f the arguments passed to it, and the caller sees the changed value. <= /p>=20
The other way that values are passed by reference is through method call=
s. This one is a little more complicated, but only a little.
Scriptom takes care of most of the scary details for you.
The basic concept is that you place the value in a wrapper object, pass = the wrapper to the method, and finally get the new value back from the wrap= per when the method returns. Scriptom's wrapper obje= ct is VariantByref (org.codehaus.groovy.scrip= tom.VariantByref).=20
One cool feature is that Scriptom allows you = to just pass a String or an Integer or wh= atever type the method requires, if you don't care about getting back the c= hanged value. Most of the time, even when a parameter is defined as b= yref, you don't really care about seeing the changed value. So you ca= n ignore all of this.=20
Okay, so let's assume that you do care. The changed value matters =
to you. Otherwise you would have stopped reading in the last paragraph, rig=
ht? Consider the following VB6 object. It simply tak=
es a byref String and modifies it.
The following example illustrates how to call the VB DLL so that the modified String is not lost.=20 =20
That is really all there is to it. Again, you shouldn't have to us= e this feature very often. But when the API is designed around byref = parameters, this can be a lifesaver!