VISUALFORCE : Get () METHOD

salesforce visualforce development

Visualforce : Get () Method 

In previous EPISODE we have discussed how to apex constructor works.

In this episode, we are gonna discuss about very interesting as well as important topic i.e. Visualforce with its two fantastic methods.

When you want to refer apex class variables in the visualforce page we need to use getter and setter methods.

get() Method :

When visualforce page wants to get the value of a variable defined in the apex class. It will invoke the get() method of that variable.

For Example :
=============================================
<apex:outputlabel >  {!Name}  </apex:outputlabel>
=============================================

In above syntax, {!Name} is a variable defoned in apex class.

In the above statement, a visualforce page is trying to use Name variable which is declared in apex class. So it will automatically invoke getName() method in the apex class.

This method will return the value of Name.

Let's understand with the help of simple apex class.

For Example :
=============================================
public class Example
{
  string Name ;

public void set(string Name) //Setter method: This will take the value from the visualforce page and stores to apex variable Name.
 {
   this.Name = Name ;
 }

public string getName() //Getter method: This method will return a value to a visualforce page whenever a Name variable is called. 
 {
   return Name ;
 }

}
=============================================

In the above class, we used set() and get() methods.

Now we will take an example for the getter method using visualforce and apex class.

Example Class :
=============================================
public class Example
{
  string Name ;

public string getName()
{
  return 'Salesforcekid' ;
}

}
=============================================

Now let's add this variable to visualforce page.


Example Visualforce Page :
=============================================
<apex:page controller="Example"> //here controller is pointing to class Example
<apex:outputlabel> {!Name} </apex:outputlabel> //Requesting to get value stored inside Name variable.
</apex:page>
=============================================

In the above code, when the page is loaded first it creates an object for the Example class.

When outputlabel calls {!Name} in the visualforce page. It invokes getName() method in the controller class.
F
finally, it will return "Salesforcekid".

Output :
=============================================
=============================================


In the above example, we can easily understand that how visualforce is getting the value from apex class or controller class.
Now as I mentioned at the start, In the previous EPISODE we understood how to create a constructor in apex class.

Let's take one more example of visualforce like the above. This time we will define a constructor in the apex class.

Apex Class of Example 2:
=============================================
public class Example2 //When visualforce page is loaded it creates an object for the Example2 class.
{
  string Name ;

public Example2() //At the time of creating the object if first calls the constructor and assign Name= "SalesforceKid".
{
 Name = "SalesforceKid" ;
}

public string getName()
{
 return Name ; //When visualforce page call {!Name} it invokes getName() in the apex class.
}

}
=============================================

Visualforce Page of Example 2:
=============================================
<apex page controller="Example2">
 <apex:outputlabel>

   My name is {!Name}

 </apex:outputlabel>
</apex:page>
=============================================

Output :
=============================================
=============================================


Cooool !! As you can observe in the above code it's very easy to the defined value of a variable inside the constructor and then refer it inside any method by using get method.

WOHOOO !! YOU HAVE JUST COMPLETED VISUALFORCE: GET () METHOD EPISODE.
If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you 
Happy learning ☁️⚡️ (Learn. Help. Share.)


<< PREVIOUS                                                       NEXT >>

VISUALFORCE : Get () METHOD VISUALFORCE : Get () METHOD Reviewed by on Rating: 5

3 comments:

  1. Loved it. Simple and easy to understand. Waiting for more episodes...

    ReplyDelete
  2. In Example 2, when does "public Example2()" happen?
    Is it when "" happens?
    Or when "public class Example2" happens?
    Or maybe "string Name ;" happens?
    Or something else altogether.

    ReplyDelete
    Replies
    1. If you your query is when does public Example2() will then the answer is whenever the class is called by default it will load the value from the constructor as constructor load at the time of initialisation.

      Delete

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.