Untreated exception (index based on 0)

Asked

Viewed 475 times

2

My idea is to make a small registration and I’m testing this to later add this data in a BD. The problem occurs on the line Console.Write("\nGenero do disco : {1}", genero);, at the time of displaying the string read via keyboard. Follow error:

Exception without treatment System.Formatexception: 'Index (based on zero) must be greater than or equal to zero and smaller than the argument list size.'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cadastro_de_músicas
{
    class Disco
    {
        public string nome;
        public string genero;
        public string ano;

        public void cadastra()
        {
            Console.Write("Digite o nome do disco: ");
            nome = Console.ReadLine();
            Console.Write("Digite o genero do disco: ");
            genero = Console.ReadLine();
            Console.Write("Digite o ano do disco: ");
            ano = Console.ReadLine();
        }

            public void exibe()
            {
            Console.Write("Nome do disco: {0}", nome);
            Console.Write("\nGenero do disco : {1}", genero);
            Console.Write("\nAno do disco: {2}", ano);
            Console.ReadKey();
        }
    }
}

2 answers

5

When writing is done with braces, these indicate the number of the element to write. Hence this line:

Console.Write("\nGenero do disco : {1}", genero);

Should be:

Console.Write("\nGenero do disco : {0}", genero);

Must be {0} because it is the first element to write from the list of elements.

If you put two variables in the same script, then you use numbers {0} and {1}, thus:

Console.Write("\nBom dia {0}, {1}", apelido, primeiroNome);

Same error happens at last typed to console.

  • Thank you very much for your help. Although I, as I said above, did not quite understand some of the details. For example: "When writing is done with braces, these indicate the number of the element to write. So this line:" That element number would be its position in memory ? We have 3 elements : 1 - name , 2 - gender and 3 - year.. Gender would be the second element instead of the first, wouldn’t it? Again, thank you :D

  • Its logic is right but there is a detail, the elements always start at 0 and not at 1. In the same way that an array of elements starts at position 0. So the first element is actually element 0, and so on

  • The number between the braces indicates the position of the element in the list of arguments passed to the String.Format() or Console.Write(). The first argument is string formatting, then the elements to interpolate: 0, 1, 2, and so on, as many as you need to pass.

3


There is a more modern way to do this that does not cause this kind of error. Make use of interpolation, is much better. I took advantage and improved other things. See nomenclature standards used in C#. There are other things that can be improved in this code, but let’s stop here.

using static System.Console;
 
namespace CadastroDeMusicas {
    public class Disco {
        public static string nome;
        public static string genero;
        public static string ano;
 
        public static void Main() {
            Write("Digite o nome do disco: ");
            nome = ReadLine();
            Write("Digite o genero do disco: ");
            genero = ReadLine();
            Write("Digite o ano do disco: ");
            ano = ReadLine();
            Exibe();
        }
 
        public static void Exibe() {
            WriteLine($"Nome do disco: {nome}");
            WriteLine($"Genero do disco : {genero}");
            WriteLine($"Ano do disco: {ano}");
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • The interpolation solved my problem, although it was not very clear how I started with. NET this week and, to tell you the truth, I should be focusing more on learning the POO than on these details. But you know curiosity, right? ;D

  • I already think the opposite, are these details that make you learn even (ñ only this, of course). OOP is overrated and at heart almost everyone learns wrong, so why rush to learn wrong? : D Has the link there talking more about interpolation.

Browser other questions tagged

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