2

I am really confused and hope somebody will be able to help me with the issue I'm having. I want to use the GET command to obtain a value from a new Form, but my code is overwriting the parameters I pass in the constructor and I am not sure why. Not very familiar with C#.

Here the script I use when I click on a specific button. It is a new Form where I pass into parameters a list of interfaces (the parameters will be modified and I do not want to):

private void btn_t1_Click(object sender, EventArgs e) {
   InterfaceT1 Formulaire_T1 = new InterfaceT1(**this.Liste_T1**);

   if (Formulaire_T1.ShowDialog() == DialogResult.OK) {
       //I WOULD WANT TO USE THE GET COMMAND HERE ONLY IF I CLICK 'OK' ON THE FORM  
   }

   Formulaire_T1.Dispose();
}

Here is the constructor of my Form Formulaire_T1 for reference:

    public InterfaceT1(List<T1> Liste_T1) {
        InitializeComponent();
        this.Liste_T1s = new List<T1>(Liste_T1); //suggested, does not change anything
        UpdateView(0);
    }

The methods I use in Interface_T1 modify the Liste_T1s but why it is also changing Liste_T1 in the main function? I do not return any value. It seems those value are now linked? I know it must be a simple thing but can't figure it out.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • If I understand your problem correctly, it sounds like you're referencing the same list in more than one place. Because of this, modifying the list in one place will be visible to the other reference as well. See here: http://stackoverflow.com/questions/13977841/how-do-i-change-my-new-list-without-changing-the-original-list EDIT: Hmm, that's not the greatest question I linked to, but the answers below describe it too. – Chris Sinclair Nov 17 '13 at 23:10
  • 3
    What is the "GET command"? – Andrew Savinykh Nov 17 '13 at 23:11
  • 2
    Please don't name your forms `Interface`. That's really really confusing. – Federico Berasategui Nov 17 '13 at 23:14
  • @Julie, when the problem occurs, are you modifying the list (a-ka adding or removing elements), or are you manipulating one of the elements inside the list? –  Nov 17 '13 at 23:32

2 Answers2

3

The reason your method in InterfaceT1 modifies the list that you pass in is that the constructor stores the reference to the list, rather than copying it. Change the constructor as follows to fix this:

public InterfaceT1(List<T1> Liste_T1) {
    InitializeComponent();
    this.Liste_T1s = new List<T1>(Liste_T1); // Make a copy
    UpdateView(0);
}

Note that this change would make a copy of the list itself, but not its elements: they would remain the same. Adding / deleting elements from the copied list will be OK, but modifying individual elements will be visible in the original. To overcome this problem, change the code as follows:

public InterfaceT1(List<T1> Liste_T1) {
    InitializeComponent();
    this.Liste_T1s = Liste_T1.Select(t => new T1(t)).ToList();
    UpdateView(0);
}

In the code above I am assuming that a new instance of T1 can be created by calling a "copy" constructor that takes another instance of T1, and copies its fields.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Okay, i think i understand! I should copy the list instead of passing it into parameteres so it will be another reference type. But your solution does not work. Is there another way to copy a list? – Julie Dupont Nov 17 '13 at 23:17
  • 1
    @JulieDupont "Does not work" does not communicate well what your problem is. – Andrew Savinykh Nov 17 '13 at 23:19
  • Same problem. Again overwriting so i think it's not really a copy. – Julie Dupont Nov 17 '13 at 23:20
  • @JulieDupont Please take a look at the edit. It is a little more complex than the previous one - try it out and see if it works. – Sergey Kalinichenko Nov 17 '13 at 23:37
  • God you are a genius. It is working perfeclty. I had to do the copy method (T1) first as your suggestion and it worked perfeclty. I don't know how i could have found it by myself. Thanks you very much!! – Julie Dupont Nov 17 '13 at 23:43
2

List is a reference type. That means that if you pass it as a parameter into another method, you're not passing a copy of the list, you're passing a reference that points back to the original list.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120