+ Start a Discussion
SaneSane 

How to get a property in contructor

Hi ,

 

Can I get property into my contructor   ? 

 

For example if I have a private list of Contacts and can I use getter methods into my contructor ?

 

The code below does not work

 

public class ExtContact {
    
    private list<contact> lstcontact;            
    
    public ExtContact(ApexPages.StandardController controller) {
          
          public list<contact> getlstcontact(){
                lstcontact=[select id,firstName,lastName from contact LIMIT 10];
                return lstcontact;
           }
    }

}

 

 

Please suggest,

 

Regards,

Sane

 

Best Answer chosen by Admin (Salesforce Developers) 
MoUsmanMoUsman
Property will be pre-evaluated in your class, When you will create an object of the class then you will be able to get values from the property. PLease follw the follwing link --http://www.salesforce.com/us/developer/docs/apex_workbook/apex_workbook.pdf to get more clarification!

--
Thanks
Usman

All Answers

MoUsmanMoUsman

Yes Sane,

You can get please follow the code give below.

 

public class ExtContact {    
    private list<contact> lstcontact;            
    //Conctructor
    public ExtContact(ApexPages.StandardController controller) {          
         lstcontact = getlstcontact;
    }

}
/*Property*/
public list<Contact> getlstcontact{
	get{
		if(lstcontact == null){
			lstcontact = new list<Contact>();
			lstcontact.addAll([select id,firstName,lastName from contact LIMIT 10]);		
		}
		return lstcontact;
	}private set;
}

If you have still issue please let me know otherwise mark as complete.

--

Thanks 

Usman

SaneSane

MoUsmanMoUsman
Property will be pre-evaluated in your class, When you will create an object of the class then you will be able to get values from the property. PLease follw the follwing link --http://www.salesforce.com/us/developer/docs/apex_workbook/apex_workbook.pdf to get more clarification!

--
Thanks
Usman
This was selected as the best answer
SaneSane
Many Thanks MoUsman !!!! :) Cheers !!!! :)
MoUsmanMoUsman
Do not forget to give Kudos!! :P