Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p><table class="wysiwyg-macro" data-macro-name="excerpt" data-macro-parameters="atlassian-macro-output-type=BLOCK|hidden=true" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2V4Y2VycHQ6aGlkZGVuPXRydWV8YXRsYXNzaWFuLW1hY3JvLW91dHB1dC10eXBlPUJMT0NLfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>how to let methods change the value of passed-in arguments</p></td></tr></table><br /> <strong>Java</strong> and <strong>Groovy</strong> are both "pass-by-value" languages. That is, if you pass a value to a method and the method modifies it, you don't see a change in the calling scope. </p> <p><strong>COM</strong>, on the other hand, supports both "pass-by-value" (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 "in/out" parameter, reflecting the direction the data is flowing. When you call a method that accepts a "pass-by-reference" parameter, the method can modify the parameter value, and this will be reflected in the calling scope. </p> <p>Byref argument passing was always a favorite of <strong>C/C++</strong> programmers, who often use the result of a method to pass exception information. It was the default way to pass values in versions of <strong>Visual Basic</strong> through <strong>6</strong>. With the advent of modern try/catch exception handling, modifying values inside a method is generally considered bad programming practice. <strong>Visual Basic</strong> (and <strong>COM</strong>) 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 are designed for scripting. That doesn't mean it will never happen, though. </p> <p>It shouldn't surprise you that <strong>Scriptom</strong> supports passing parameters by-reference, just in case you need it. </p> <p>There are actually two ways that values are passed by-reference. For <a class="confluence-link" href="/display/GROOVY/COM+Events" data-linked-resource-id="27328520" data-linked-resource-type="page" data-linked-resource-default-alias="COM Events" data-base-url="http://docs.codehaus.org">COM Events</a>, which do not support returning values, the only way to pass back information is through a byref parameter. That is all explained in the article, and it is actually pretty transparent. You don't have to do anything special for it to work. Your event handler can just change one of the arguments passed to it, and the caller sees the changed value. </p> <p>The other way that values are passed by reference is through method calls. This one is a little more complicated, but only a little. <strong>Scriptom</strong> takes care of most of the scary details for you.</p> <h1>How Does it Work?</h1> <p>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 wrapper when the method returns. <strong>Scriptom's</strong> wrapper object is <strong>VariantByref</strong> (<strong>org.codehaus.groovy.scriptom.VariantByref</strong>). </p> <p>One cool feature is that <strong>Scriptom</strong> allows you to just pass a <strong>String</strong> or an <strong>Integer</strong> or whatever type the method requires, if you don't care about getting back the changed value. Most of the time, even when a parameter is defined as byref, you don't really care about seeing the changed value. So you can ignore all of this. </p> <p>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, right? Consider the following <strong>VB6</strong> object. It simply takes a byref <strong>String</strong> and modifies it. <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Option Explicit Public Sub StringByrefDemo(ByRef A As String) A = A & " VB6 was here!" End Sub </pre></td></tr></table> <p>The following example illustrates how to call the <strong>VB DLL</strong> so that the modified <strong>String</strong> is <em>not</em> lost.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Scriptom.inApartment { def testObj = new ActiveXObject("MyDemo.PassByrefDemo"); VariantByref svalue = new VariantByref("This is Groovy!"); testObj.StringByrefDemo(svalue); assert svalue.value == "This is Groovy! VB6 was here!"; } </pre></td></tr></table> <p>That is really all there is to it. Again, you shouldn't have to use this feature very often. But when the API is designed around byref parameters, this can be a lifesaver!</p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced