C
C#3mo ago
jacks

i need blazor help

i'm trying to fill an array of players but the inputs are going blank as soon as you deselect them and i don't know why
No description
12 Replies
jacks
jacks3mo ago
if there are any fixes or any workarounds please lmk
jacks
jacks3mo ago
No description
Joschi
Joschi3mo ago
It would be great if you could paste your code here instead of sending screenshots. That makes it way easier to test your code and find out what's wrong. $code
MODiX
MODiX3mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Joschi
Joschi3mo ago
When numPlayer == 4 and you try to change the value in any input field you should get an exception. What does that exception say?
jacks
jacks3mo ago
@page "/"
@using Project3.Data
@using Project3.Services
@inject IWarService warService

<h1>War Game</h1>

<select @bind=numPlayers>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
@for (int i = 0; i < numPlayers; i++)
{
<div>
Player #@(i+1): <input @bind="@players[i]"/>
</div>
}
<button @onclick="ConfirmPlayers">Start New Game</button><br />
<div>@errorMessage</div>

@code
{
int numPlayers = 0;
string[] players = new string[4];
string errorMessage = "";

public void ConfirmPlayers()
{
errorMessage = "";

if (numPlayers < 2)
{
errorMessage = "Please select the number of players!";
}
else
{
for (int i = 0; i < numPlayers; i++)
{
if (players[i] == "")
{
errorMessage = "Please enter a name for all players!";
}
}
}
}
}
@page "/"
@using Project3.Data
@using Project3.Services
@inject IWarService warService

<h1>War Game</h1>

<select @bind=numPlayers>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
@for (int i = 0; i < numPlayers; i++)
{
<div>
Player #@(i+1): <input @bind="@players[i]"/>
</div>
}
<button @onclick="ConfirmPlayers">Start New Game</button><br />
<div>@errorMessage</div>

@code
{
int numPlayers = 0;
string[] players = new string[4];
string errorMessage = "";

public void ConfirmPlayers()
{
errorMessage = "";

if (numPlayers < 2)
{
errorMessage = "Please select the number of players!";
}
else
{
for (int i = 0; i < numPlayers; i++)
{
if (players[i] == "")
{
errorMessage = "Please enter a name for all players!";
}
}
}
}
}
jacks
jacks3mo ago
No description
jacks
jacks3mo ago
No description
jacks
jacks3mo ago
☹️
Joschi
Joschi3mo ago
See you get an index out of bounds error. What does that tell you about the issue you are facing? Which parts of your code can produce this specific exception? You really should try figuring this out by yourself, because it's an sneaky error that can cause a lot of annoyance and it's worth to understand it. A code snippet to exactly see what's wrong
<select @bind="@_numberOfPlayers">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>


<div>
<h5>For Loop</h5>
<br/>

@for (_loopI = 0; _loopI < _numberOfPlayers; _loopI++)
{
<div>
<span>Player index is @_loopI</span>
<span>Player # @(_loopI + 1): <input @bind="@_playerNames[_loopI]"/></span>
</div>
}
@foreach (var index in Enumerable.Range(0, 5))
{
<div>
<span>Value of index @index : @_playerNames[index]</span>
</div>

}

The index you are currently modifying with each input field: @_loopI
</div>


@code {
private int _numberOfPlayers = 0;
private readonly string[] _playerNames = new string[5];
private int _loopI = 0;
}
<select @bind="@_numberOfPlayers">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>


<div>
<h5>For Loop</h5>
<br/>

@for (_loopI = 0; _loopI < _numberOfPlayers; _loopI++)
{
<div>
<span>Player index is @_loopI</span>
<span>Player # @(_loopI + 1): <input @bind="@_playerNames[_loopI]"/></span>
</div>
}
@foreach (var index in Enumerable.Range(0, 5))
{
<div>
<span>Value of index @index : @_playerNames[index]</span>
</div>

}

The index you are currently modifying with each input field: @_loopI
</div>


@code {
private int _numberOfPlayers = 0;
private readonly string[] _playerNames = new string[5];
private int _loopI = 0;
}
The solution / explanation Basically what happens is that all your inputs are bound to the loop variable i. And because that increases one more time even after the for condition is fullfilled they try to modify players[4] which does not exist. And even if that wouldn't be the case they are all bound to the exact same index. You got two options to fix this. Either you copy i into a local variable and bind player[yourLocali]. Or you use foreach with an Enumerable.Range like in the example above.
jacks
jacks3mo ago
this makes a lot of sense, thanks for the help! i was able to solve the issue just by doing this
@for (int i = 0; i < numPlayers; i++)
{
int pos = i;

<div>
Player #@(i+1): <input @bind="@players[pos]"/>
</div>
}
@for (int i = 0; i < numPlayers; i++)
{
int pos = i;

<div>
Player #@(i+1): <input @bind="@players[pos]"/>
</div>
}
so yea like you said just copying i to a local variable thank you a lot !!!
Joschi
Joschi3mo ago
Good job! No Problem