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
mpolimpoli 

Why isn't my variable being incremented?

Hi, I'm completely new to developing in Apex and Visualforce, so please bear with my elementary question.

 

I have a simple repeat block in my Visualforce page:

 

<apex:repeat value="{!stagesStrLst}" var="eachstage" id="theRepeat">
      {!curStageNum} {!eachstage} {!stageStr}

      <br/>
</apex:repeat>

 

'stagesStrLst' is a String list obtained from a SOQL query in my Apex code (8 elements in the list).

'stageStr' is a String variable with the following get method:

get
{
   String blockStr;
   blockStr = '<div class="workarea"> ';
   blockStr += '<h3>' + curStage + '</h3>';
   blockStr += ' </div>';
           
   System.debug(curStageNum);

   curStageNum++;

   System.debug(curStageNum);

   return blockStr;   
}

 

'curStage' simply returns the element of 'stagesStrLst' of index 'curStageNum', which is set to 0 in the controller's constructor. Every time stageStr's get method is called, it should increment curStageNum.

 In other words, what I think it should output on my Visualforce page is

0 a a

1 b b

2 c c

3 d d

4 e e

5 f f

6 g g

7 h h

 

where a, b... are the string elements in the list.

 

However, I instead get

0 a a

0 b a

0 c a

0 d a

0 e a

0 f a

0 g a

0 h a

It's looping through stagesStrLst just fine, but it seems curStageNum is never being incremented properly. In fact, the system log shows that the debug statements I inserted only output once (though the second debug statement reveals that curStageNum did indeed get incremented to 1, but that's never shown in the Visualforce page).

 

I'm pretty confused about this behavior and was wondering if someone could please explain why my Visualforce page never shows my integer variable being incremented? This was just a small test project to try get myself to understand what I can and can't do with Visualforce/Apex before I tackle a bigger, more functional project.

 

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
wesnoltewesnolte

Hey

 

I've had this issue before and ended up using a wrapper instead. I would like to resolve it though because it should work. Let's try a few not so obvious things:

 

 Change your get{} for the public property into a proper getter eg.getStageStr()(I've had some issues concerning properties before). Ensure that you also have a setter method defined.

 

 Change curStageNum++; into curStageNum= curStageNum +1; 

 

I know that doesn't make much sense but sometimes you have to try obscure things. Any way it does seem like a bug, it's almost as though the get property thinks that the currStageNum var is local to the method and loses it's value after each method call completes. That and the method only seems to be called once for the entire repeat block.

 

If none of that works try writing a wrapper class inside you controller. Something along the lines of

 

public class myWrapper{

public Integer curStageNum{get;set;}

public String eachStage{get;set;}

public String stageStr{get;set;}

 

public myWrapper(Integer c, String e, String s){

  curStageNum = c;

eachStage = e;

stageStr = s;

 

 

 

Somewhere in your controller you'd instantiate a list of these wrappers passing each the appropriate constructor values. Your repeat would then iterate over a list of these wrappers. 

 

Cheers,

Wes 

All Answers

wesnoltewesnolte

Hey

 

I've had this issue before and ended up using a wrapper instead. I would like to resolve it though because it should work. Let's try a few not so obvious things:

 

 Change your get{} for the public property into a proper getter eg.getStageStr()(I've had some issues concerning properties before). Ensure that you also have a setter method defined.

 

 Change curStageNum++; into curStageNum= curStageNum +1; 

 

I know that doesn't make much sense but sometimes you have to try obscure things. Any way it does seem like a bug, it's almost as though the get property thinks that the currStageNum var is local to the method and loses it's value after each method call completes. That and the method only seems to be called once for the entire repeat block.

 

If none of that works try writing a wrapper class inside you controller. Something along the lines of

 

public class myWrapper{

public Integer curStageNum{get;set;}

public String eachStage{get;set;}

public String stageStr{get;set;}

 

public myWrapper(Integer c, String e, String s){

  curStageNum = c;

eachStage = e;

stageStr = s;

 

 

 

Somewhere in your controller you'd instantiate a list of these wrappers passing each the appropriate constructor values. Your repeat would then iterate over a list of these wrappers. 

 

Cheers,

Wes 

This was selected as the best answer
mpolimpoli

Hi Wes,

 

I tried the various methods you suggested, and the list of wrappers seems to work. Thanks so much!

 

Michael