You need to sign in to do that
Don't have an account?
Visualforce: static class variables don't stick
I have visualforce snippet:
<apex:repeat value="{!prosets}" var="project"> <td> <apex:image width="180" height="110" url="{!URLFOR($Action.Attachment.Download, attId)}"/> <h3><a href="{!SourceSite}/project?id={!project.Id}" target="_parent">{!project.Name}</a></h3> <p>By: {!project.Account__r.Name} ImgNum: {!ImgNum}</p> </td> </apex:repeat>
and controller snippet:
public static Integer ImgNum = 0; public Integer getImgNum() { return ImgNum; } public Id getAttId() { if (pageImages.size() > ImgNum) { Id ret = pageImages[ImgNum].Id; ImgNum = ImgNum + 1; return ret; } else { return '00PR0000000GVWg'; } }
Now - obviously, I want to return an incremented index in the pageImages list - but when I run this baby, ImgNum does not get incremented (confirmed by the getImgNum getter)
Why? Are static variables not compitable with Visualforce controllers?
I'm not sure why you'd want this as a static variable, but I don't think that is the problem.
The VF Developer's Guide mentions in a few places that your getter methods should not have side effects, as the order of execution is not guaranteed.
I've found that when iterating a list, I have to ensure that each element in the list contains all the information that I need for the row being produced. Thus rather than having a getAttId method that calculates information for the image, this is calculated when the list is created included in the list entry.
All Answers
I'm not sure why you'd want this as a static variable, but I don't think that is the problem.
The VF Developer's Guide mentions in a few places that your getter methods should not have side effects, as the order of execution is not guaranteed.
I've found that when iterating a list, I have to ensure that each element in the list contains all the information that I need for the row being produced. Thus rather than having a getAttId method that calculates information for the image, this is calculated when the list is created included in the list entry.