Monday, September 16, 2013

Getter and setter methods - What are they??

Well, once you start using a controller or an extension you will get used to these words...

So, it is good that we understand what these methods really do..

Getter and setter methods are used to pass data from your visualforce page to your controller and vice versa..

Let's take a very simple scenario... Let's assume you want to display a textbox in your visualforce page.. When the user enters some value in this textbox and clicks on a button you want the value entered by the user in your Apex class (ie. basically your controller or extension)

So go ahead and create a simple visualforce page.. the code for this would be

<apex:page controller="simplegetset">
<apex:form>
<apex:outputlabel value="Enter your name here"/>
<apex:inputtext value="{!userinput}"/>
</apex:form>
</apex:page>


The Apex code for this page would be...

public class simplegetset
{
public String userinput{get; set;}
}


Now, the variable "userinput" in your Apex class will store the value entered by the user....

Let's analyze the methods now...

Get

The "get" method is used to pass data from your Apex code to your Visualforce page.. In our example we are not passing any value.. hence, when your page loads initially the textbox will have a empty value...

So, lets modify our code and pass a default value to the textbox.. Change the Apex code as follows..

public class simplegetset
{
public String userinput;
public String getuserinput(){return 'John';}

public void setuserinput(String userinput)
{
this.userinput = userinput;
}
}


You can now see that your page loads with a value 'John'...

Set

The "set" method is used to pass values from your visualforce page to the controller... In our example the variable "userinput" will be storing the value entered in the textbox..

Now modify your VF page as below..

<apex:page controller="simplegetset">
<apex:form>
<apex:outputlabel value="Enter your name here"/>
<apex:inputtext value="{!userinput}">
<apex:actionsupport event="onclick" rerender="display" />
</apex:inputtext>
<apex:outputpanel id="display">
<apex:outputtext value="The name entered is {!userinput}"/>
</apex:outputpanel>
</apex:form>
</apex:page>




The Apex code would be...

public class simplegetset
{
public String userinput{get; set;}
}


In this example what happens is.. the variable "userinput" stores the value entered in visualforce page and passes it to the Apex code.. hence you are able to see the entered value in the visualforce page..

I guess you might understand what i am saying.. to make things simple now use the same visualforce page.. but modify the Apex code as below..

public class simplegetset
{
public String userinput;
public String getuserinput(){return 'John';}

public void setuserinput(String userinput)
{
this.userinput = userinput;
}
}


Now, whatever value you enter the page always displays "The name entered is John".... This is because your get method always returns 'John'... but still your set method will store the value you entered in the variable "userinput"....


Hope i did not confuse...

No comments:

Post a Comment