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();
}
}
}
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
– wes85melis
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
– Isac
The number between the braces indicates the position of the element in the list of arguments passed to the
String.Format()orConsole.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.– Wtrmute