You need to sign in to do that
Don't have an account?
setter method for array variable
Hi All,
I am not sure if this is possible or not.
Budgets is being accessed in a form as input text fields from a visualforce page.
public Integer[] Budgets {get;set;}
public Integer[] getBudgets() {
return Budgets;
}
public void setBudgets(????) {
???
}
Is it possible to write a setter method as well? If it is, can someone let me know the syntax? If not possible, can you point me to a workaround? Ideally, I do not want to define 12 individual Integer variables to do the same thing.
The visualforce code looks something like this:
<apex:inputText value="{!Budgets[0]}" size="9"/>
<apex:inputText value="{!Budgets[1]}" size="9"/>
<apex:inputText value="{!Budgets[2]}" size="9"/>
<apex:inputText value="{!Budgets[3]}" size="9"/>
<apex:inputText value="{!Budgets[4]}" size="9"/>
<apex:inputText value="{!Budgets[5]}" size="9"/>
<apex:inputText value="{!Budgets[6]}" size="9"/>
<apex:inputText value="{!Budgets[7]}" size="9"/>
<apex:inputText value="{!Budgets[8]}" size="9"/>
<apex:inputText value="{!Budgets[9]}" size="9"/>
<apex:inputText value="{!Budgets[10]}" size="9"/>
<apex:inputText value="{!Budgets[11]}" size="9"/>
Thanks in advance!!!
I guess this means that integers are immutable or similar. That being the case, our old friend wrapper classes can help here.
Here's the controller with a simple integer wrapper:
and the page:
All Answers
I wrote a blog post a while ago regarding persisting list edits (lists and arrays being the same thing in Apex) which should give you a few pointers:
http://bobbuzzard.blogspot.com/2011/03/persisting-list-edits-in-visualforce.html
Hi Bob,
Thank you for your quick reply. Your code makes perfect sense, and something very similar worked for me when I am persisting Salesforce standard and custom objects.
In this case, I am just trying to do the same with an array variable (or a List) in a controller and it does not seem to work.
For example:
Apex Code:
---------------
public with sharing class TestController {
public Integer[] Budgets {get;set;}
public Integer x {get;set;}
public TestController() {
Budgets = new Integer[3];
for(integer i = 0; i < 3 ; i++) {
Budgets[i] = 2000;
}
x = 1000;
}
public PageReference save() {
// do something
// return to a different page
PageReference pg = new PageReference('/home/home.jsp');
return pg;
}
}
Visualforce Code:
-----------------------
<apex:page Controller="TestController">
<apex:form >
<apex:pageBlock title="Test" mode="edit">
<apex:pageBlockButtons location="top">
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Test" >
<apex:inputText value="{!budgets[0]}" size="9"/>
<apex:inputText value="{!budgets[1]}" size="9"/>
<apex:inputText value="{!budgets[2]}" size="9"/>
<apex:inputText value="{!x}" size="9"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
When I click on "Save" button, the above does not work and the page remains there as before. However, if I remove the three lines consisting of budgets and only keep the line with the "x" variable, clicking on "Save" returns to the home.jsp as expected.
Following works:
----------------------
<apex:page Controller="TestController">
<apex:form >
<apex:pageBlock title="Test" mode="edit">
<apex:pageBlockButtons location="top">
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Test" >
<apex:inputText value="{!x}" size="9"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
What am I doing wrong with the array variable? Please note that since I am not using a Salesforce object field, I have to use inputText instead of inputField.
Thanks again!
Not sure why it isn't working - maybe something not quite right with dynamic visualforce bindings.
There shouldn't be any difference with processing a list of primitives versus a list of sobjects. What happens if you have:
Hi Bob,
Thanks for your quick response again. I used the code you sent and I get the following error in the page editor:
Error: Unknown property 'budget'
<apex:page Controller="TestController">
<apex:form >
<apex:pageBlock title="Test" mode="edit">
<apex:pageBlockButtons location="top">
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Test" columns="3">
<apex:repeat value="{!budgets}" var="budget">
<apex:inputText value="{!budget}" size="9"/>
<apex:inputText value="{!budget}" size="9"/>
<apex:inputText value="{!budget}" size="9"/>
</apex:repeat>
<apex:inputText value="{!x}" size="9"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
I guess this means that integers are immutable or similar. That being the case, our old friend wrapper classes can help here.
Here's the controller with a simple integer wrapper:
and the page:
Hi Bob,
Thank you so much for your solution. Now it works exactly the way I wanted. Thanks for your time. I appreciated.:smileyhappy:
ssoftware