MiguelPT
MiguelPT

Reputation: 29

Can't add values to List (Count is always 1) - C#

Many times I had to create lists of structures to store my data in C#. But this time, I have a problem, I can't seem to be able to add values to my list. 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 agua;
    }        

    public mng ()
    {
    }

 public void addValues(double transportes, double agua)
    {            
        Despesas dia = new Despesas();
        try
        {
            dia.transportes = transportes;
        }
        catch
        {
            MessageBox.Show("Error", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

         try
        {
            dia.agua = agua;
         }
        catch
        {
            MessageBox.Show("Error", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

         try
         {
             dias.Add(dia);
         }
         catch
         {
             MessageBox.Show("Error", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }

        MessageBox.Show("Added: " + dias.Count);
        day++;
    }

and on Form1:

 private void button1_Click(object sender, EventArgs e)
    {
   try{     
    double transportes = Convert.ToDouble(txtTransportes.Text);
    double agua = Convert.ToDouble(txtAgua.Text);
    mng mngV = new mng();
             mngV.addValues(transportes, agua);
       }
   catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
       }

On the overload constructor I have a message that shows the count of the list values every time a new one is added and guess value is always 1 and I just don't understand why.

The only thing different from this project to the other similar projects I've done and work is that this project has 2 forms, I don't think that has anything to do with my problem.

Upvotes: 1

Views: 276

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502066

On the overload constructor I have a message that shows the count of the list values every time a new one is added and guess value is always 1 and I just don't understand why.

That's because each time you create a new instance of mng (which should be given a more meaningful and conventional name, by the way) you're creating a new list, which is empty to start with. You're adding one item to it, so the count is 1.

If you want to share a single list between multiple instances of mng, you should possibly make the dias variable static (in which case it's not associated with any specific instance) or you could pass a reference into the constructor.

Alternatively, perhaps you don't really want to create a new instance of mng at all - perhaps you should be calling a method on an existing instance? It's hard to know without more context.

As a side-note, I would strongly urge you to avoid public fields and mutable structs (your Despesas type).

Upvotes: 4

Related Questions