function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sankar GaneshSankar Ganesh 

Addition in visualforce

Since I am a I have some struggle.
I have to get two numbers and my  labels  for that is value1 and value2.I have another button called submit when I press that I should get sum.
How should I get a sum here.
My Apex code of two numbers

public class Sample{
public Integer userinput{ get;set;}
public Integer userinput1{ get;set;}
public Integer sum(){
Integer c=userinput+userinput1;
return c;
}
public pageReference getsum()
{
  sum(); 

  return null; 

}    

}

My visualforce page

<apex:page controller="Sample" >
 <apex:form >
 <apex:pageBlock >

<apex:outputLabel >Value1</apex:outputLabel>
<apex:inputText value="{!userinput}"/> <br/><br/>
<apex:outputLabel >Value2</apex:outputLabel>
<apex:inputText value="{!userinput1}"/>
<apex:commandButton value="submit" action="{!getsum}"/><br/><br/>
<apex:outputLabel >sum</apex:outputLabel>
<apex:outputField value="{}" />

 </apex:pageBlock>
 </apex:form>
</apex:page>
Prateek Singh SengarPrateek Singh Sengar
Try making the following changes
  • Create a public Integer variable call it as result
Public Integer result {get;set;}
  • In getSum() assign the value to the variable result
result = sum();
  • In you visualforce in outputField refer to this variable result
<apex:outputField value="{!result}" />
  • Add re-render attribute to the button (Optional)
Hope this helps