Sunday, 9 July 2017

Write an apex class to demonstrate setter method i.e, passing the values and saving the values to apex variables

Write an apex class to demonstrate setter method i.e, passing the values and saving the values to apex variables


Apex class: 

    Public class example1 
         { 
           Public string name; 
           Public string gets Name ()  
             { 
               return name; 
                      } 
               Public void set Name (string name) 
                  { 
                     This name = name; 
                          } 
                   } 
VF page: 

<apex: page controller = “example1”> 
<apex: from> 
<apex: output Label> Enter Name </apex: output Label>
<apex: input Text value = “{! Name}” /= 
<apex: command button value = “click” retender = “one” /> 
<apex: output Label id = “one” > your name is {! Name} </apex: output Label > 
</apex: from >
</apex: page >

we can also define setter and getter methods in a single line.

      Public integer {set; get;} 


How to all call the apex methods in a visual force page: 


      Public class Demo 
        { 
          Public page reference show () 
              { 
                 Return null; // when we give return null it will come to the back to the same page   
                       } 
             } 
<apex: command button value = “click” action = “{! show}” /> 


  • when we click on the “click” button it will invoke page reference show () method. 
  • page reference is the return type of every method that we have called from visual force page. 


       Public class example1 
         { 
            Public string name; 
             Public string get name () 
                 {  
                 return name; 
                            } 
            Public void set Name (string name) 
                     { 
                       This.name = name; 
                             } 
         Public reference show () 
                   { 
                       name = ‘this is my name’ + name; 
                    return null; 
                               }
                       } 
<apex: page controllers = “example1”> 
<apex: from > 
<apex: output Label > enter name </apex: output Label > 
<apex: command button value = “{! name}”/> 
<apex: command button value = “click” retenders = ‘’one’’ action = “{! show}”/> 
<apex: output Label id = “one” > {! name} </apex: output Label > 
<apex: form> 
<apex: page> 

Ex: simple apex to class to perform addition and subtraction based on the button you have clicked.

      Public class example1 
           { 
              Public integer b value {get; set;} 
              Public integer a value {get; set;} 
             Public integer result {get; set;}
             Public String operation {get; set;}  
             Public page reference sub () 
                         { 
                             Result = value – b value; 
                             Operation = ‘subtraction ‘; 
                             Return null;
                                  } 
             Public page reference sub () 
                         { 
                           result = a value = b value;
                           operation = ‘ADDITION’; 
                            return null; 
                                } 
                           } 

<apex: page controllers = “example1”> 
<apex: form > 
<apex: page block title = “calculator”> 
<apex: page block section columns = “1” title= “simple operations” collapsible = “false”> 
<apex: page block section item>   
<apex: output Label> enters A value < /apex: output Label> 
<apex: input Text value = “{! a value}” />
</apex: page block section Item> 
<apex: page block section Item>  
<apex: output Label> Enter B value <apex: output Label> 
<apex: input Text value = “{! B value}”/> 
<apex: page block section Item> 
<apex: page block section item> 
<apex: output Label> you have performed {! operation} 
             Of {! value} and {! b value} and the results 
             {! result} </apex: output Label> 
             <apex: page block section Item> 
             <apex: page block section> 
             <apex: page block> 
             <apex: form> 
             <apex: ….> 

No comments:

Post a Comment