Search code examples
c#webblazor-webassembly

Cannot increase the counter inside a for loop


<div class="forecast-list">
    @for (int i=1; i<13;i++)
    {
         <div style="position:relative;left:@onScreenPosition;">@CalcRevenuesChange(Incomes[i].Quantity,Forecast[i].Quantity) </div>
            onScreenPosition += 2;

    }
</div>

the counter still remains 0, I tried to increase him inside @CalcRevenuesChange() but still doesn’t work. The output that I expected for is: 0 0 0 0 0 0 0 0 0 0 0 0 0 0

but it's still remains 0000000000000 ........................................


Solution

  • Variables created inside element will always be reset. Try this.

     @{
            int count=1;
            <div class="forecast-list">
                @for (; count<13;count++)
                {
                     <div style="position:relative;left:@onScreenPosition;">@CalcRevenuesChange(Incomes[count].Quantity,Forecast[count].Quantity) </div>
                        onScreenPosition += 2;
            
                }
            </div>
    }