APIs for Java Developers

Go to APIs for .NET Developers

com.javonet.api.keywords

Class NRef


  • public class NRef
    extends NOut
    A class that represents .NET ref keyword @see http://msdn.microsoft.com/en-us/library/14akc2c7.aspx

    The ref keyword causes an argument to be passed by reference, not by value. The effect of passing by reference is that any change to the parameter
    in the method is reflected in the underlying argument variable in the calling method. This value of a reference parameter is always the same as the
    underlying argument variable.

    To use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword, as shown in the following example:

    .NET class with method expecting argument passed by reference

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

    Sample usage on JAVA side

    To call this method from JAVA using Javonet method argument must be explicitly wrapped with NRef class.
    Both primitive types or primitive types arrays and other .NET objects (NObject) or NObject arrays might be passed by reference.
    Because Java does not support passing by reference, primitive types must be wrapped in “AtomicReference<?>” class.

      
     NObject refEx = Javonet.New("RefExample");
     //Wrap Java integer in AtomicReference to allow passing by reference
     AtomicReference{@literal <}Integer{@literal >} myInt = new AtomicReference{@literal <}Integer{@literal >}(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.
     
     

    An argument that is passed to a ref parameter must be initialized before it is passed. This differs from out parameters, whose arguments do not have to be explicitly initialized before they are passed. For more information, see NOut.

    You can use passing by reference for any JAVA primitive type (String, Integer, Float, Boolean, etc…) and arrays of these objects as well as NObject variables and arrays of NObject.

    Passing arguments with ref keyword is supported both for instance and static methods.

    Since:
    1.2
    Version:
    1.4hf14


    • Constructor Summary

      Constructors 
      Constructor and Description
      NRef(java.util.concurrent.atomic.AtomicReference<?> argument)

      Constructor used to wrap primitive type argument with “ref” keyword.
      NRef(NEnum argument)

      Constructor used to wrap NEnum argument with “ref” keyword.
      NRef(NObject argument)

      Constructor used to wrap NObject argument with “ref” keyword.
      NRef(NObject[] argument)

      Constructor used to wrap NObject array argument with “ref” keyword.



    • Method Summary



      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait



    • Constructor Detail



      • NRef

        public NRef(java.util.concurrent.atomic.AtomicReference<?> argument)
             throws JavonetException
        Constructor used to wrap primitive type argument with “ref” keyword.
        Parameters:
        argument – AtomicReference instance that wraps argument variable for call with “ref” keyword.
        Throws:
        JavonetException – if wrong argument is passed



      • NRef

        public NRef(NEnum argument)
             throws com.javonet.internal.commons.lang.NullArgumentException
        Constructor used to wrap NEnum argument with “ref” keyword.
        Parameters:
        argument – Argument to be passed to method with “ref” keyword.
        Throws:
        com.javonet.internal.commons.lang.NullArgumentException – if wrong argument is passed
        Since:
        1.4hf14



      • NRef

        public NRef(NObject argument)
             throws JavonetException
        Constructor used to wrap NObject argument with “ref” keyword.
        Parameters:
        argument – Argument to be passed to method with “ref” keyword.
        Throws:
        JavonetException – if wrong argument is passed



      • NRef

        public NRef(NObject[] argument)
             throws JavonetException
        Constructor used to wrap NObject array argument with “ref” keyword.
        Parameters:
        argument – Argument to be passed to method with “ref” keyword.
        Throws:
        JavonetException – if wrong argument is passed