Passing Arguments by Reference with “ref” and “out” Keywords

Passing Arguments by Reference with “ref” and “out” Keywords

Microsoft .NET allows you to create methods that expect arguments to be passed by reference. Passing by reference is introduced by explicitly setting ref or out keywords before argument type within the method definition. For example, void MyMethod(ref int arg1).

Note: Passing arguments by reference should not be confused with passing reference-type arguments described in the previous section. If you need just to pass another .NET object in method arguments, please see the previous section.

JavOnet allows you to pass by reference both .NET objects (NObject) or primitive types (String, Integer, Float, Boolean etc..). You can also pass arrays of these types like NObject[] or String[].

While passing variables by reference, the value of that variable might be modified within the method body. Read more about .NET ref and out keywords here:

JavOnet allows you to invoke methods expecting “ref” or “out” arguments by providing two dedicated types: NRef and NOut. To pass the variable by reference, you must wrap the variable with the NRef or NOut class. For NObject and NObject[], you can wrap them with NRef or NOut directly. However because Java does not support passing by reference primitive types, both variables (like String, Integer, Boolean etc.) or arrays of them should be wrapped with the “AtomicReference<?>” class, and then wrapped with NRef or NOut object.

Example .NET object with expecting “ref” argument method

class RefExample
{
	public void Method(ref int i)
	{
		i = i + 44;
	}
}

Example of a “ref” argument method from Java using javOnet

 NObject refEx = Javonet.New("RefExample");
 //Wrap Java integer in AtomicReference to allow passing by reference
 AtomicReference<Integer> myInt = new AtomicReference<Integer>(10);

 refEx.invoke("Method",new NRef(myInt));

 System.out.println(myInt.get());
 //Output will display number "55" because int passed by reference has been modified within the method body.

See Live Example!

Example of a Int32.TryParse (string arg, out int number) method from Java

 String strNumber = "4";
 AtomicReference<Integer> myInt=new AtomicReference<Integer>();

 //NOut constructor with argument type is used because myInt has NULL value. Without specifying explicitly 
 //argument type .NET would not be able to locate proper method to execute.
 if (Javonet.getType("Int32").invoke("TryParse",strNumber,new NOut(myInt,"System.Int32")))
 {
 	System.out.println(myInt.get());
 }

Example of an “out” argument method to populate an array of custom .NET objects

 
class Item 
{ 
     public String ItemName { get; set; } 
} 
class PopulateItems 
{ 
     public void Populate(out Item[] items) 
     { 
           items = new Item[5]; 
           for (int i=0; i<5; i++) 
           { 
                items[i]=new Item(); 
                items[i].ItemName="Item"+i.ToString(); 
           } 
     }
}

Example of a populate method passing items in an array with an “out” argument

The populate method expects to pass an items array with an “out” argument. Therefore the items array does not need to be initialized before calling the method, and after execution, the passed variable will be filled with items.

 
NObject populator = Javonet.New("PopulateItems"); 
//Wrap null Java array in atomic reference to pass the reference as out argument
AtomicReference<NObject[]> items = new AtomicReference<NObject[]>(null);

populator.invoke("Populate", new NOut(items,"Item[]"));

//After execution of this method local items array variable will contain five items generated within Populate method body.

See Live Example!