0

I want to add objects in a List <T> when I select an item from a combobox, named cmbMaxim. The problem is I obtain a list with the same object (last object added). It's seems like new operator won't work and I obtain a reference to the same object. Here is the code:

public partial class Form1 : Form
{
    List<Varianta> lvar = new List<Varianta>();
    int contor = 0;
    public Form1()
    {
        InitializeComponent();
    }


    private void cmbMaxim_SelectedIndexChanged(object sender, EventArgs e)
    {
        int[] vgen = new int[6];
        int vmax = Convert.ToInt32(cmbMaxim.Text);
        for (int i0 = 1; i0 < vmax - 4; i0++)
            for (int i1 = i0 + 1; i1 < vmax - 3; i1++)
                for (int i2 = i1 + 1; i2 < vmax - 2; i2++)
                    for (int i3 = i2 + 1; i3 < vmax - 1; i3++)
                        for (int i4 = i3 + 1; i4 < vmax; i4++)
                            for (int i5 = i4 + 1; i5 < vmax + 1; i5++)
                            {
                                Varianta var = new Varianta();
                                vgen[0] = i0;
                                vgen[1] = i1;
                                vgen[2] = i2;
                                vgen[3] = i3;
                                vgen[4] = i4;
                                vgen[5] = i5;
                                contor++;
                                var.Var = vgen;
                                var.Index = contor;
                                lvar.Add(var);
                            }
    }
}

Class Varianta is:

class Varianta
{
    int[] var = new int[6];
    int index;
    int scor=0;
    int eliminate=0;

    public int Scor
    {
        get 
        { 
            return scor;
        }
        set 
        { 
            scor = value; 
        }
    }

    public int Index
    {
        get 
        { 
            return index; 
        }
        set 
        { 
            index = value; 
        }
    }


    public int [] Var
    {
        get 
        { 
            return var; 
        }
        set 
        { 
            var = value; 
        }
    }
}

What is wrong?

5
  • 1
    If Var already has an array allocated, why are you overwritting it with something that is shared across all of the loops? Instead why not just var.Var[0] = i0;, etc.? Commented Jul 17, 2015 at 19:05
  • 4
    6 nested loops?? Good luck debugging !! Commented Jul 17, 2015 at 19:06
  • 1
    nothing quite like 6 layers deep of for loops... Commented Jul 17, 2015 at 19:06
  • move the declaration of vgen to the innermost loop. All you objects points to the same array. Commented Jul 17, 2015 at 19:14
  • As a side note, since you allocate var already in Varianta, I would personally make Var's set to be private (i.e. you can't assign a new array, but you can access and set the values in the array). Commented Jul 17, 2015 at 19:21

2 Answers 2

1

Since you are creating int[] vgen out of the loop, every time you do lvar.Add(var) (where var.Var = vgen), you are pointing to the same variable (int[] vgen).

But at the same time, at every loop you are changing the values of int[] vgen (overwriting the previous ones).

The code is not ok at all. As other people pointed out, 6 nested loops will be a nightmare to debug. But that being said, I guess one simple solution is to declare int[] vgen inside the loop and it will work for you.

Edit: as @crashmstr pointed out, another way to make it work is using var.Var[0] = i0 and so on (instead of using vgen[0] = i0). No need to redeclare int[] vgen inside the loop since it is already initialized inside Varianta.

Sign up to request clarification or add additional context in comments.

2 Comments

No need to declare vgen at all, each Varianta already allocates an array, so can just set those values directly.
The 6 loops was only for generating the array vgen []. It's a way to generate combination of n numbers in series of 6. No other code will be there. I'm sure there is a better way to do this, but I haven't found it.
0

You need to clear list first. Currently the event adds the items one after another.

    private void cmbMaxim_SelectedIndexChanged(object sender, EventArgs e)
    {
        lvar.Clear();
        int[] vgen = new int[6];
        ...

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.