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
CreatobugCreatobug 

Is this race condition? whats the solution?

I have just started writing apex controller and I'm quite new to it.....and I'm not sure what is happening here....I wanted the output of the VF page to be 1 2 3 4 but instad it is 1 1 1 1.  Is there a work around for this?......I need the values within the <repeat>tag itself. 

<!-- The VF PAGE -->

<apex:page controller="increment" sidebar="false" showHeader="false" >
     <apex:repeat var="a" value="{!a}">
         {!i} <br/>
     </apex:repeat>
</apex:page>


/* The Controller ----Increment */

public class increment
{
    integer i=0;
    list<integer> a{get;set;}
      
    public integer geti()
    {
       
        i++;
        return i;
    }
    public list<integer> geta()
    {
        a = new list<integer>();
        a.add(10);
        a.add(10);
        a.add(10);
        a.add(10);
        return a;
    }
}


Best Answer chosen by Admin (Salesforce Developers) 
hchhch

Use the Following Code:

 

Page:

<apex:page controller="increment" >
     <apex:repeat var="a" value="{!a}">
         {!a} <br/>
     </apex:repeat>
</apex:page>

 

 

Class:

public class increment
{   
   list<integer> a;
    public list<integer> geta()
    {
        a = new list<integer>();
        for(integer i=1;i<5;i++)
            a.add(i);      
        return a;
    }
}

All Answers

hchhch

Use the Following Code:

 

Page:

<apex:page controller="increment" >
     <apex:repeat var="a" value="{!a}">
         {!a} <br/>
     </apex:repeat>
</apex:page>

 

 

Class:

public class increment
{   
   list<integer> a;
    public list<integer> geta()
    {
        a = new list<integer>();
        for(integer i=1;i<5;i++)
            a.add(i);      
        return a;
    }
}

This was selected as the best answer
aballardaballard

For performance reasons, VF generally will not call the same getter multiple times while rendering a page; it caches the value returned the first time. So the value from the first geti vcall was used each time. 

 

So you need something like wht hch suggests, returning a list of values to iterate over. 

CreatobugCreatobug

Ya this is fine....Thankyou......but I'm still still stuck with my requrements.........I am basically returning an object list to a repeat tag and diplaying some details about the object within the repeat tag........now the thing is , I need to add a radio button for each of these set of details : 

 

Eg

<radio 1>

            details........  // this is printed from the object I am passing to the repeat tag

<radio 2>

            details.......

<radio 3>

            details........

So I wanted the 1 ,2,3,4 to supply as indices to the radio buttons (The radio vales are defined in the controller)......but since I am already using a <repeat tag for the object (details)......I cant pass the values to the repeat tag like you suggested........

 

Could you think about a workaround for this?.......I hope I am clear with the explaination of my problem....I'll try and post the code soon (its a bit messy right now....let me clean it up)....:)

 

CreatobugCreatobug

Hmmmm Thanks.....Thats Interesting....so you basicall should never put values that change within the get method......but this creates quite a bit of complications :(

aballardaballard

I think you are making this more complicated than it needs to.  Your repeat iterates over the list.  The var in the repeat references each element of the list in turn, (By the way, I strongly suggest you name the var something different from the controller property specified in the value attribute).  Within the repeat you use the var to access the element.   No need for the separate incremented variab le at all as far as I can see.