function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
abhik dey 1abhik dey 1 

Visualforce Getter Issue

VF Page:

<apex:page sidebar="false" controller="asd">
<apex:form id="abc">
<apex:actionFunction name="a1" action="{!checkparam}" reRender="foo">
<apex:param id="aname" value="" name="param1" assignTo="{!g1}"/>
</apex:actionFunction>
{!g1}
</apex:form>
<button type="button" onclick="a1('page1')">Click Me </button>

</apex:page>

Controller:

public class asd {
    public string g1{
    get {
    g1='getter';
    return g1;
    }
    set{
    g1='setter';
    }
    }
public asd(){
g1='cons';
}
    public void checkparam() {
    system.debug('$$$'+g1);
    }

}

Above are the page and the controller,on clicking the button click me I am passing the parameter to a Apex method in the controller.
but evrytime I got "getter" value in the debug statement in the method checkparam (above class).
Is it like whatever is their in the getter method I will be getting that value only in the methods irrespective of any modification???
Now if I change my code to :

public class asd {
    public string g1{get ;set}
public asd(){
g1='cons';
}
    public void checkparam() {
    system.debug('$$$'+g1);
    }

}

I am able to get the page value in the controller method debug statement.

Can anyone have any idea on this behaviour???

 
sivaextsivaext
Hi, 

Flow of execution of getter and setter method is 

1. First setter will execute and write value (as per first block code, on click button , it will execute setter method and set the value  i.e. setter.)
2. Second getter will excuete and read the value ( then it will excecute  getter method and override value  with "getter")

final debug statement print is "getter"

In second block code , you are not define any value in getter and setter methods.

Regards
Siva.
 
Shaijan ThomasShaijan Thomas
abhik dey 1abhik dey 1
Thanks guys for the answers.
So, does it mean like if I have define any hardcoded value in my getter method in the controller then in the entire controller that variable will have the value 'getter'(as per the above example). And whatever value i set in the page against that variable , will never be read in the controller methods (Method: checkparam) or any other methods in the same controller???
Also , for the first piece of code above , in the debug log i see that the varaible value gets override with the page value i.e. Page1 but still while I print that varaible under the method (checkparam), it shows 'getter' as the value.

Got confused..so can u explain that??
Shaijan ThomasShaijan Thomas
When you open the page your getter method will execute. when you leave the page or refresh the page setter method will execute. Even you set in the page level, the hardcoded value will set finally.
Thanks
Shaijan