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
FinnArildFinnArild 

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?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

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

bob_buzzardbob_buzzard

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. 

This was selected as the best answer
FinnArildFinnArild
You are quite right - the way I envisioning doing this wouldn't work at all. I made a collection-class instead and returned an array of that.