ERROR The index was outside the matrix limits (System.Indexoutofrangeexception)

Asked

Viewed 5,597 times

3

     //Esse código tem o objetivo de substituir uma letra por um código

string usuario = Environment.UserName;

            string palavra = usuario.ToLower();
            int tamanho_array = palavra.Length;
            var array = new string[tamanho_array];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = palavra[i].ToString();
            }
            tamanho_array++;
            int n = 0;
                string saida = "";
            while (n != tamanho_array){
                if(array[n] == "a"){saida += "-01100001-";};
                if(array[n] == "b"){saida += "-01100010-";};
                if(array[n] == "c"){saida += "-01100011-";};
                if (array[n] == "d"){saida += "-01100100-"; };
                if (array[n] == "e"){saida += "-01100101-"; };
                if (array[n] == "f"){saida += "-01100110-"; };
                if (array[n] == "g"){saida += "-01100111-"; };
                if (array[n] == "h"){saida += "-01101000-"; };
                if (array[n] == "i"){saida += "-01101001-"; };
                if (array[n] == "j"){saida += "-01101010-"; };
                if (array[n] == "k"){saida += "-01101011-"; };
                if (array[n] == "l"){saida += "-01101100-"; };
                if (array[n] == "m"){saida += "-01101101-"; };
                if (array[n] == "n"){saida += "-00001010-"; };
                if (array[n] == "o"){saida += "-01101111-"; };
                if (array[n] == "p"){saida += "-01110000-"; };
                if (array[n] == "q"){saida += "-01110001-"; };
                if (array[n] == "r"){saida += "-01110010-"; };
                if (array[n] == "s"){saida += "-01110011-"; };
                if (array[n] == "t"){saida += "-01110100-"; };
                if (array[n] == "u"){saida += "-01110101-"; };
                if (array[n] == "v"){saida += "-01110110-"; };
                if (array[n] == "w"){saida += "-01110111-"; };
                if (array[n] == "x"){saida += "-01111000-"; };
                if (array[n] == "y"){saida += "-01111001-"; };
                if (array[n] == "z"){saida += "-01111010-"; };
                if (array[n] == "."){saida += "-00101110-"; };
                    n++;
            }

Exit "-01100001-01101100-01100101-01111000-" If the user is "Alex" but the copier returns the error The index was outside the matrix boundaries (System.Indexoutofrangeexception) and I inspected and the array is filled with the user name correctly.

1 answer

3


You added value to the size_array variable after it was popular, remove ++ or change the while condition to (n < size_array)

        string usuario = Environment.UserName;

        string palavra = usuario.ToLower();
        int tamanho_array = palavra.Length;
        var array = new string[tamanho_array];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = palavra[i].ToString();
        }

        //retirar o ++ caso a condição ficar a mesma
        tamanho_array++;
        int n = 0;
            string saida = "";

        //caso precisar desse valor tamanho_array++; altere o while
        // para while (n < tamanho_array)
        while (n != tamanho_array){
            if(array[n] == "a"){saida += "-01100001-";};
            if(array[n] == "b"){saida += "-01100010-";};
            if(array[n] == "c"){saida += "-01100011-";};
            if (array[n] == "d"){saida += "-01100100-"; };
            if (array[n] == "e"){saida += "-01100101-"; };
            if (array[n] == "f"){saida += "-01100110-"; };
            if (array[n] == "g"){saida += "-01100111-"; };
            if (array[n] == "h"){saida += "-01101000-"; };
            if (array[n] == "i"){saida += "-01101001-"; };
            if (array[n] == "j"){saida += "-01101010-"; };
            if (array[n] == "k"){saida += "-01101011-"; };
            if (array[n] == "l"){saida += "-01101100-"; };
            if (array[n] == "m"){saida += "-01101101-"; };
            if (array[n] == "n"){saida += "-00001010-"; };
            if (array[n] == "o"){saida += "-01101111-"; };
            if (array[n] == "p"){saida += "-01110000-"; };
            if (array[n] == "q"){saida += "-01110001-"; };
            if (array[n] == "r"){saida += "-01110010-"; };
            if (array[n] == "s"){saida += "-01110011-"; };
            if (array[n] == "t"){saida += "-01110100-"; };
            if (array[n] == "u"){saida += "-01110101-"; };
            if (array[n] == "v"){saida += "-01110110-"; };
            if (array[n] == "w"){saida += "-01110111-"; };
            if (array[n] == "x"){saida += "-01111000-"; };
            if (array[n] == "y"){saida += "-01111001-"; };
            if (array[n] == "z"){saida += "-01111010-"; };
            if (array[n] == "."){saida += "-00101110-"; };
                n++;
        }
  • Here it worked out thank you very much, as I did not see it? hahaha.

  • 1

    @Gabr13l_alm31da relaxes, it comes with time, the more you do but you see these mistakes faster ^^.

Browser other questions tagged

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