Action<IContainer> strange behavior #773
-
I've been trying to create a Action ColumnDescriptor (materialTable) and mount its items before applying it to the page content, but I've been incountering some strange errors and even super crazy behaviors I've never imagine could be possible on debugging such as like code GOING BACKWARDS haha. does someone know what I'm doing wrong? as soon as I add the "item" to the materialTable, It breakes, and the output on the actual pdf is just pure LSD |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
At a guess, i.e. for (int i = 0; i < materialProducts.Count - 1; i++)
{
string color = "...";
materialTable += x =>
{
var index = i;
TableItem(x.Item(), index);
};
} |
Beta Was this translation helpful? Give feedback.
-
The problem is related to the scope of the You have declared this variable before the for loop, so it exists as a single reference and is updated by each loop iteration. Lambda expression captures this variable, but as declared in the outer scope, it will store only the last item value from a lambda perspective. However, if you put your variable inside a loop, its scope will differ, and a separate reference will be created for each loop iteration. Therefore, your lambda will capture the correct expected value (and you will likely get a warning about capturing variables and why it may introduce some unexpected pressure on GC). You have posted a screenshot instead of code, so it is challenging to show the difference 😀 |
Beta Was this translation helpful? Give feedback.
The problem is related to the scope of the
item
variable.You have declared this variable before the for loop, so it exists as a single reference and is updated by each loop iteration. Lambda expression captures this variable, but as declared in the outer scope, it will store only the last item value from a lambda perspective.
However, if you put your variable inside a loop, its scope will differ, and a separate reference will be created for each loop iteration. Therefore, your lambda will capture the correct expected value (and you will likely get a warning about capturing variables and why it may introduce some unexpected pressure on GC).
You have posted a screenshot instead of code, so…