index outside range c#

Asked

Viewed 1,668 times

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:

inserir a descrição da imagem aqui

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();
                }
            }
       }
    }
  • 1

    The image does not open for me. Prefer to place it directly inside your question by uploading by Stack Overflow.

  • @Victorstafusa she appears to you now? I put her on Imgur to make it easy to view directly on the site.

  • What is telaBuilder and where is this stated? How do you declare counter? When you use telaBuilder[counter], you access an array in an invalid position.

  • @Yes, now come to me.

  • @Victorstafusa added the statements

  • @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?

  • 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?

  • I think the StringBuilder at the point where you are trying to change the letter is empty.

  • 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...

Show 4 more comments

1 answer

1

You are trying to assign a letter to a position that does not exist in Stringbuilder.

To do this you must initialize Stringbuilder with a number of positions equal to the word you want to discover:

private StringBuilder telaBuilder;
private int numeroDeLetras = 10;

...
...
telaBuilder = new StringBuilder(numeroDeLetras);

On the other hand, traditionally, the game usually represents the letters not yet found with a -.

For that use

telaBuilder = new StringBuilder(new string('-', numeroDeLetras));

See working on .NET Fiddle

  • I tried to do this, but it makes the following mistake: a field initializer cannot reference the nonstatic field method or property

  • If you are declaring the variables at class level, in this case you cannot inhibit them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.