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.

3
  • 1
    The array is defined to hold only one value, Carro []carros = new Carro[1]; that's why you are getting that error Commented Oct 13, 2017 at 12:47
  • 1
    Possible duplicate of What is an "index out of range" exception, and how do I fix it? Commented Oct 13, 2017 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<Carro> instead. Commented Oct 13, 2017 at 13:04

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

2 Comments

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!
I used this, but it doesn't seem to be working, It says "Cannot implicitly convert type 'System.Collections.Generic.List<CriarCarroForm.Carro>' to 'CriarCarroForm.Carro[]' "
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.

6 Comments

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.
Oh yes! Thanks, that was so dumb of me to not to realize that!
@DonBoitnott Hey thanks for pointing it out:D, I'm really a beginner, even lower.
@DonBoitnott What do you mean by acrobatic indexing though?
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.
|
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.