1
I am trying to make an assignment of value in a certain index of a String, however, I get the following error that I do not understand the reason:
System.Argued tofrangeexception: 'The index was out of range. It should be non-negative and smaller than the collection size.*'
string tela;
int counter = 0;
StringBuilder telaBuilder = new StringBuilder();
private void btnSendWord_Click(object sender, EventArgs e)
{
char letra = Convert.ToChar(txtGetWord.Text);
Boolean codigoVerificador;
codigoVerificador = verificador.VerificaLetra(comboPalavra[0],letra);
if (codigoVerificador == true)
{
foreach(char c in comboPalavra[0].ToCharArray())
{
counter = counter + 1;
if(c == letra)
{
telaBuilder[counter] = Convert.ToChar( letra);
tela = telaBuilder.ToString();
}
}
}
}
The image does not open for me. Prefer to place it directly inside your question by uploading by Stack Overflow.
– Victor Stafusa
@Victorstafusa she appears to you now? I put her on Imgur to make it easy to view directly on the site.
– user28595
What is
telaBuilder
and where is this stated? How do you declarecounter
? When you usetelaBuilder[counter]
, you access an array in an invalid position.– Victor Stafusa
@Yes, now come to me.
– Victor Stafusa
@Victorstafusa added the statements
– dev-john
@Victorstafusa just clarifying, I used Stringbuilder to try to change a character of a String, since C# does not allow this directly... But I’m bumping into this mistake, it seems obvious, but I don’t understand why, what might be?
– dev-john
I don’t understand what it is you’re trying to do with this code. What represents the letter you’re trying to put on
telaBuilder
?– Victor Stafusa
I think the
StringBuilder
at the point where you are trying to change the letter is empty.– Victor Stafusa
The code is for a hangman game, so when the user enters with a letter and clicks the button, that letter will be saved in the "letter" variable, and the telaBuilder is the Stringbuilder object I’m trying to use to insert that letter into the Hangman Game String, if the user hits the letter...
– dev-john