You need to sign in to do that
Don't have an account?
Chandrasekhar Reddy 5
Expected output?
I have below controller:
, , Method2,
But I am expecting output : , , Method2,Method2
Why so? Why is cString value not retained though it is being set in getStringMethod2 method?
public class MyController { public string cString { get; set;} public string getStringMethod1(){ return cString; } public string getStringMethod2(){ if(cString == null) cString = 'Method2'; return cString; } }VF:
<apex:page controller="MyController"> {!cString}, {!StringMethod1}, {!StringMethod2}, {!cString} </apex:page>Actual Output:
, , Method2,
But I am expecting output : , , Method2,Method2
Why so? Why is cString value not retained though it is being set in getStringMethod2 method?
Have a look at the order of execution for VF pages.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_get_request.htm
The page expressions are evaluated and the values are returned using the get method
In your case, when the VF engine goes through the page.
1. {!cString} - at this point value is null - so print null.
2. {!StringMethod1} - still value is null - print null
3. {!StringMethod2} - in this getter method, the value of cstring is set to "Method2", so print "Method2".
4. {!cString} - now the value is "Method2" as it was set in step3, so print "Method3".
I can see that you are just experimenting here, but I would recommend to set the values of your variables during the page initialize using a constructor.
Thanks for your reply. I am not sure what is 'Method3' in your reply. My hunch is that it is a typo error. I thought the same way as you explained.
But the actual output is not It prints the output Per your explanation, you are also expecting the output as I am My question is, how is getting printed , , Method2, instead of , , Method2,Method2 ?
i know its late, but this may help other readers.
https://salesforce.stackexchange.com/questions/169596/need-help-understanding-get-set-methods-weird-behavior