+ Start a Discussion
Erik RodgersErik Rodgers 

Where is getProducts() method being invoked in the lesson "Using Inner Classes" of the Apex Workbook?

Can anyone explain where the getProducts() method is actually being invoked in the lesson "Using Inner Classes" of the Apex Workbook? I do not see anywhere in the Visualforce markup, or the StoreFrontController (constructor or otherwise) where this method gets invoked.

http://www.salesforce.com/us/developer/docs/apex_workbook/Content/apex_visualforce_5.htm
 
Best Answer chosen by Erik Rodgers
KaranrajKaranraj
Getter and setter methods are used to pass data from your visualforce page to your controller. Below is the one of method of initiating getter and setter variable in apex controller
public class apexControllerClass{
 public List<DisplayMerchandise>  products{get; set;}
}
In the above method it will initiate the gettter and setter variable products, when the page loads it won't contains any value unless u assign it the controller constructor.

If you want to initiate some value to the variable while initiating you can also initiate the variable in the below method.The "get" method is used to pass data from your Apex code to your Visualforce page. Here "get" is the keyword and Products is the variable name. So don't need to call this method, it will be invoked when the variable initiated
List<DisplayMerchandise> products; 
public List<DisplayMerchandise> getProducts() { 
     if(products == null) { 
         products = new List<DisplayMerchandise>(); 
         for(Merchandise__c item : [ SELECT Id, Name, Description__c, Price__c, Total_Inventory__c FROM Merchandise__c]) { 
            products.add(new DisplayMerchandise(item)); 
          } 
      } 
      return products; 
 }


 

All Answers

KaranrajKaranraj
Getter and setter methods are used to pass data from your visualforce page to your controller. Below is the one of method of initiating getter and setter variable in apex controller
public class apexControllerClass{
 public List<DisplayMerchandise>  products{get; set;}
}
In the above method it will initiate the gettter and setter variable products, when the page loads it won't contains any value unless u assign it the controller constructor.

If you want to initiate some value to the variable while initiating you can also initiate the variable in the below method.The "get" method is used to pass data from your Apex code to your Visualforce page. Here "get" is the keyword and Products is the variable name. So don't need to call this method, it will be invoked when the variable initiated
List<DisplayMerchandise> products; 
public List<DisplayMerchandise> getProducts() { 
     if(products == null) { 
         products = new List<DisplayMerchandise>(); 
         for(Merchandise__c item : [ SELECT Id, Name, Description__c, Price__c, Total_Inventory__c FROM Merchandise__c]) { 
            products.add(new DisplayMerchandise(item)); 
          } 
      } 
      return products; 
 }


 
This was selected as the best answer
Erik RodgersErik Rodgers
So, just to clarify, it auto-magically associates the getter method “getProducts” to the controller class variable “products” so that when “products” is referenced in VisualForce by the component, the controller knows to invoke the getter method adhering to the same naming convention as the variable? The properties syntax makes sense to me, but the getter setter approach seems strange, but I just need to get used to it. Thank you for your answer!! Erik
David Roberts 4David Roberts 4
I am struggling to create a test class for the StoreFrontController.

list <StoreFrontController.DisplayMerchandise> mylist = StoreFrontController.getProducts();
is fine but I can't get an acceptable syntax for the get methods.

e.g. string myname = mylist[1].name;