1

I have simple class structure that has an array part of its fields. I want to store this class in a list. The problem is when adding additional class structures to the list it handles it as if it's a direct update and not referenced.

 public class TrayLayout
 {
      public int[] inerD { get; set; }

public class TrayLayout
 {
      public int[] inerD { get; set; }

      public TrayLayout(int[] inerD)
      {
           this.inerD = inerD;
       }
 }
 public partial class Form1 : Form
 {
      public List<TrayLayout> trayL = new List<TrayLayout>();
      public Form1()
      {
           InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
           int[] aa = new int[2];
           aa[0]=1;
           aa[1]=2;
           //add the new class to TrayLoayout
           trayL.Add(new TrayLayout(aa));
           aa[0]=3;
           aa[1]=4;
           //add the new class to TrayLoayout using input array
           trayL.Add(new TrayLayout(aa));
           aa[0]=5;
           aa[1]=6;
           //add the new class to TrayLoayout
           trayL.Add(new TrayLayout(aa));
           textBox1.Text = "the numbers accepted \n"+ trayL[0].inerD[0].ToString() + " , " +trayL[0].inerD[1].ToString() + " \n" + trayL[1].inerD[0].ToString() + " , " +trayL[1].inerD[1].ToString() + " \n" + trayL[2].inerD[0].ToString() + " , " +trayL[2].inerD[1].ToString() ;

      }

I get in TextBoxes it shows the last input 5,6 5,6 5,6 instead of 1,2 3,4 5,6. I must be missing something?

0

4 Answers 4

2

You always refer to the same int array, overwriting the previous values.

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

Comments

0

You have created 1 ArrayObject aa. And you are overwriting the first 2 values, when you assign 3 and 4. And when you assign 5 and 6 you have overwritten 3 and 4.

Create 3 different arrays or make aa[0] to aa[5].

Comments

0

Assigning the int array to the TrayLayout class does not make a copy. Each instance of the TrayLayout class in your example has a reference to the same array, so when you update it following the call to the TrayLayout constructor, all your instances of TrayLayout see the same values.

Either copy the array internally in the TrayLayout constructor, or (as others are suggesting) use different arrays in the constructor calls.

2 Comments

I tried in the constractor public TrayLayout(int[] ainerD) { this.inerD[0] = ainerD[0]; this.inerD[1] = ainerD[1]; } and nothing was writin
You are still only capturing a reference. Use something like this.inerD = new int[ainerD.Length]; Array.Copy(ainerD, this.inerD, ainerD.Length);
0

You need to replace this:

int[] aa = new int[2];
aa[0]=1;
aa[1]=2;
//add the new class to TrayLoayout
trayL.Add(new TrayLayout(aa));
aa[0]=3;
aa[1]=4;
//add the new class to TrayLoayout
trayL.Add(new TrayLayout(aa));
aa[0]=5;
aa[1]=6;
//add the new class to TrayLoayout
trayL.Add(new TrayLayout(aa));

With this:

trayL.Add(new TrayLayout(new int[]{1,2}));
trayL.Add(new TrayLayout(new int[]{3,4}));
trayL.Add(new TrayLayout(new int[]{5,6}));

Edit

You could do it like this:

var start=1;
var end=13;
trayL.Add(new TrayLayout(Enumerable.Range(start,end).ToArray()));

3 Comments

what if I have an array 0f 53 that will be heck of coding is there a simple way
trayL.Add(new TrayLayout(new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13})); and so on... not convenint
Arion 10X 4 the comment but what I shown here is a recreate of the problam, where in the project I have to store 10 barcodes or in other case 53 barcodes IN AN ARRAY and store the class in List<> so is there a esay way to store the data?

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.