简体   繁体   中英

Strange variable behaviour in Blazor wasm

My Blazor Component:

@for(int i = 0; i < buildings.Length; i++)
{
    int localI = i;
    bool enoughRessources = true; // local to the loop

    BuildingRequirement[] requirements = db.BuildingsRequirements
        .Include(x => x.GameItem)
        .Where(x => x.EntityId == buildings[localI].BuildingId)
        .ToArray();

@foreach (BuildingRequirement requirement in requirements)
{
       bool localEnoughRessources = IsThereEnoughRessources(requirement);
       enoughRessources &= localEnoughRessources;
}   

<MudText>@enoughRessources</MudText>
<MudText>@(buildingUpgradeProcess is null)</MudText>

@if (enoughRessources && *other bool here*)
{
    <MudButton Color="Color.Tertiary" OnClick="() => Upgrade(buildings[localI])">Upgrade</MudButton>
}
else if (*other bool*)
{
    <MudButton Color="Color.Warning">Already upgrading another building</MudButton>
}
else
{
    <MudButton Color="Color.Error">Not enough ressources</MudButton>
}

}

I'm having troubles with variables in my component. I want to change the button displayed depending on the bool value of "enoughRessources" but the value seems to change mid component.

Inside the

<MudText>@enoughRessources</MudText>

it will display true, but then for some itterations of the loop, just under when the code hits the @if(enoughRessources), it will evaluate as false.

I have no multithreaded code in my @code section which could interfere, i'm only doing some initial db request in OnInitializedAsync().

Some other things that i have tried with no difference: a foreach loop, moving the enoughRessource bool to an array of bool ( one for each building ) declared in the @code section

Why do this variable changes mid course? Am i missing something? Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM