Find your content:

Search form

You are here

Primitive Method arguments by reference

 
Share

From the documentation:

        Passing Method Arguments By Value
   In Apex, all primitive data type arguments, such as Integer or String, 
   are passed into methods by value. This means that any changes to the arguments
   exist only within the scope of the method. When the method returns, the changes
   to the arguments are lost.

Example:

class A {

    x=1;

    B.setx(x);

 --here i want x=2---
}

class B{
   public setx(int x){
     x=2;
   }
}

How can i pass a primitive value to a function by reference?

Thanks in advantage for any advice.


Attribution to: Enry

Possible Suggestion/Solution #1

As it states, primitives are passed by value. There is no syntax for passing references explicitly. Instead, pass a map or a wrapper class in:

Void increment(map<object, integer> values) {
    For(object key: values.keyset())
        Values.put(key, values.get(key)+1);
}

The results will be visible to the caller via the map.


Attribution to: sfdcfox
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32323

My Block Status

My Block Content