• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 15-Day Money-Back Guarantee

Java Equivalent to VB 'ByRef' Parameters

Java does not support VB-style 'ByRef' parameters. All Java parameters are passed by value. However, you can wrap the parameter type in another type (we call it 'RefObject') which still allows changes internal to the object.

Here's a simple example:

VB:

Public Sub refParamMethod(ByRef i As Integer)
    i = 1
End Sub

Public Sub callRefParamMethod()
    Dim i As Integer = 0
    refParamMethod(i)
End Sub

Java:

public void refParamMethod(tangible.RefObject<Integer> i)
{
    i.argValue = 1;
}

public void callRefParamMethod()
{
    int i = 0;
    tangible.RefObject<Integer> tempRef_i = new tangible.RefObject<Integer>(i);
    refParamMethod(tempRef_i);
    i = tempRef_i.argValue;
}

package tangible;

//----------------------------------------------------------------------------------------
// Copyright © 2007 - 2017 Tangible Software Solutions Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
//
// This class is used to replicate the ability to pass arguments by reference in Java.
//----------------------------------------------------------------------------------------
public final class RefObject<T>
{
    public T argValue;
    public RefObject(T refArg)
    {
        argValue = refArg;
    }
}


Use VB to Java Converter to convert from VB to Java.     


Copyright © 1997 – 2017 Tangible Software Solutions Inc.