0

Many times I had to create lists of structures to store my data in C#. But this time, I have an error, "Index Out of Range". I don't quite understand why, since I already did similar projects, but I would be very appreciated if someone could help me.

class mng
{
    int day = 0;
    List<Despesas> dias = new List<Despesas>();

    public struct Despesas
    {
        public double transportes;
        public double alimentacao;
        public double vestuario;
        public double agua;
        public double luz;
        public double educacao;
    }        

    public mng ()
    {
    }

(This is where I get the error)

    public void showMessage()
    {
        for (int i = 0; i < 31; i++)
        {
            MessageBox.Show("Água: " + dias[i].agua + "\nTransportes: " + dias[i].transportes);
        }
    }

and on Form1:

    double transportes = Convert.ToDouble(txtTransportes.Text);
    double agua = Convert.ToDouble(txtAgua.Text);
    mng mngV = new mng(transportes, agua, educacao);

    if (day < 31)
        {
            button1.Enabled = false;
            //this is the button that enables the ShowMessage() Method.
        }
        else
        {
            button1.Enabled = true;
        }

I never execute the method showMessage() before the List has 31 values, so why is it out of Index? I tried to search on the site first but couldn't find any questions with a similar problem although there are a lot with the same error. I changed the overload constructor to:

    public mng(double transportes, double agua)
    {            
        Despesas dia = new Despesas();
        dia.transportes = transportes;
        dia.agua = agua;
        dias.Add(dia);
        MessageBox.Show("Added: " + dias.Count);
        day++;
    }

and guess what, the dias.Count is always 1. I don't understand why...

MiguelPT
  • 31
  • 1
  • 6
  • 3
    are you sure that you never call the method before it has 31 values? do you have any code to show that this is the case? could you not change the loop to: `for (int i = 0; i < dias.Count; i++)` – Ric Dec 30 '14 at 11:53
  • Yes, I updated the question with the code. – MiguelPT Dec 30 '14 at 11:58

4 Answers4

0

Try changing your method to:

public void showMessage()
{
  for (int i = 0; i < dias.Count; i++)
  {
      MessageBox.Show("Água: " + dias[i].agua + "\nTransportes: " + dias[i].transportes);
  }
}
w.b
  • 11,026
  • 5
  • 30
  • 49
  • That way it does not show nothing at all, but that means that it is not adding anything to the list, right?? – MiguelPT Dec 30 '14 at 12:06
  • If it doesn't show anything, your list is probably empty at the time of the call, so problem is somewhere where you add items to the list – w.b Dec 30 '14 at 12:14
0

What about trying this:

public void showMessage()
{
  for (int i = 0; i < dias.Count; i++)
  {
    MessageBox.Show("Água: " + dias[i].agua + "\nTransportes: " + dias[i].transportes);
  }
}

or even better:

public void showMessage()
{
    foreach(var d in dias)
    {
        MessageBox.Show(... + d.agua + .... + d.transportes);
    }
}
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
0

Just change your code this way:

for (int i = 0; i < dias.Count; i++)

And you can check it under the debugger to verify how many items are there in the array when the loop is running.

msporek
  • 1,187
  • 8
  • 21
0

make sure that dias has 31 values.you can use foreach instead of for as it will give the the same result with much better performance

    foreach(var item in dias )
   {
    MessageBox.Show("Água: " + item.agua + "\nTransportes: " + item.transportes);
   }

if you are using for- than problem is because your list might not filled properly have less items

Nayas Subramanian
  • 2,269
  • 21
  • 28