0

I'm trying to add textbox and numericUpandDown values to an array, but it doesn't seem to be working.

Carro []carros = new Carro[1];
private Carro carro;

public Form1()
{
    ..
}

private void Form1_Load(object sender, EventArgs e)
{
    ..
}

private void AdicionarCarro()
{
    this.carro = new Carro(textboxCor.Text, textboxMarca.Text, textboxModelo.Text,
        (int.Parse(numUpDownCilindrada.Text)), (int.Parse(numUpDownVelocidade.Text)));
}

private Carro[] AdicionarArray(Carro carro, Carro[] array)
{
    AdicionarCarro();

    int novoTamanho = array.Length + 1;

    Carro[] carros = new Carro[novoTamanho];

    for (int i = 0; i < array.Length; i++)
    {
        carros[i] = array[i];
    }

    carros[novoTamanho] = carro;


    return carros;
}

private void buttonGravar_Click(object sender, EventArgs e)
{
    AdicionarArray(carro, carros);
}

When I type the values and click on the "buttonGravar", it gives me this Error:

Error

I'd be much delighted to get some tips/help on it.

Draken
  • 3,134
  • 13
  • 34
  • 54
ckvywk
  • 19
  • 4
  • 1
    The array is defined to hold only one value, `Carro []carros = new Carro[1];` that's why you are getting that error – sujith karivelil Oct 13 '17 at 12:47
  • 1
    Possible duplicate of [What is an "index out of range" exception, and how do I fix it?](https://stackoverflow.com/questions/24812679/what-is-an-index-out-of-range-exception-and-how-do-i-fix-it) – SᴇM Oct 13 '17 at 12:50
  • You are forgetting to use the return value of AdicionarArray(). It is just a fundamentally wrong way to do this, and not just because you forgot to update the carros variable, change its declaration to `List` instead. – Hans Passant Oct 13 '17 at 13:04

3 Answers3

6

Using System.Collection.Generic.List<T> would be much simpler, since it doesn't have a fixed size:

List<Carro> carros = new List<Carro>();
carros.AddRange(array);
carros.Add(carro);
return carros;
Ian H.
  • 3,840
  • 4
  • 30
  • 60
Joe
  • 80,724
  • 18
  • 127
  • 145
  • I'm still a student, and we still haven't been on that part yet (If I remember it correctly), thanks for the alternative solution though! – ckvywk Oct 13 '17 at 13:01
  • I used this, but it doesn't seem to be working, It says "Cannot implicitly convert type 'System.Collections.Generic.List' to 'CriarCarroForm.Carro[]' " – ckvywk Oct 13 '17 at 13:18
3

Better way:

private List<Carro> Carros;

public Form1()
{
    Carros = new List<Carro>();
    ..
}

private void Form1_Load(object sender, EventArgs e)
{
    ..
}

private void AdicionarCarro()
{
    var carro = new Carro(textboxCor.Text, textboxMarca.Text, textboxModelo.Text,
        (int.Parse(numUpDownCilindrada.Text)), (int.Parse(numUpDownVelocidade.Text)));
    Carros.Add(carro);
}

private void buttonGravar_Click(object sender, EventArgs e)
{
    AdicionarCarro();
}

To help you understand your code:

carros[novoTamanho] = carro;

should be

carros[novoTamanho - 2] = carro;

Reason:

Array index starts from 0. novoTamanh represents new length (starting at 1, not 0 unlike index), which is outside array.

Yahya
  • 3,386
  • 3
  • 22
  • 40
  • 4
    All of that acrobatic indexing is begging someone to point out just how horrible that code really is, and how it needs a complete overhaul, instead of more bandaids. – DonBoitnott Oct 13 '17 at 12:56
  • Oh yes! Thanks, that was so dumb of me to not to realize that! – ckvywk Oct 13 '17 at 12:57
  • @DonBoitnott Hey thanks for pointing it out:D, I'm really a beginner, even lower. – ckvywk Oct 13 '17 at 12:58
  • @DonBoitnott What do you mean by acrobatic indexing though? – ckvywk Oct 13 '17 at 12:58
  • Sorry to be so blunt, but I tend to suggest better fixes, not just how to dodge the problems one makes for themselves...beginner or no. As to your question...I mean if you find the need to do fancy indexing like is being suggested, it typically means you're abusing the array, or mishandling its overall usage. – DonBoitnott Oct 13 '17 at 12:59
  • @Bips04 what DonBoitnott means is don't try to play snooker with chop sticks, you can still do it, but it's not the right way. – Yahya Oct 13 '17 at 13:06
0

It's an index out of range exception because your array Carro is of size tmanho:

Carro[] carros = new Carro[novoTamanho];

and carros can contain exactly "novoTamanho" items indexed from "0" to "novoTamanho -1"

You can simply solve this by defining:

int novoTamanho = array.Length + 2;

Or if you do not want to manage indexes, use Lists:

List<Carro> listCarro = new List<Carro>;
listCarro.AddRAnge(array);
listCarro.Add(carro); 
return listCarro.ToArray();