List of Arrays of Lines

Hello

Im trying to create array of 2 lines and place them in a List. (I want to display then separately).

I cant understand why the 1st array seems to get overwritten with the 2nd.

List <Line> xtLines = new List<Line>();

Line[] lns = new Line[2]; // One array of  2 lines.

// Create 2 lines:
lns[0] = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 1));
lns[1] = new Line(new Point3d(2, 2, 0), new Point3d(3, 3, 1));

xtLines.Add(lns); // Add the lns array to the list

// Create another 2 lines:
lns[0] = new Line(new Point3d(4, 4, 0), new Point3d(5, 5, 1));
lns[1] = new Line(new Point3d(6, 6, 0), new Point3d(7, 7, 1));

xtLines.Add(lns); // Add the lns array to the list

A = xtLines[0]; // Out the first array but this lot is the same as xtLines[1]?

B = xtLines[1]; // ie last lot OVERWRITES the previous lns array?

Ive spent ages trying to see why

Thanks in advance

expt.gh (6.3 KB)

How about this?

public class LinePair
{
  public Line First { get; set; }
  public Line Second { get; set; }
}

...

var item = new LinePair
{
  First = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 1)),
  Second = new Line(new Point3d(2, 2, 0), new Point3d(3, 3, 1))
};
xtlines.Add(item);

– Dale

lns is not re-created, the first and only instance of the lns array always remains intact inside the xtLines, only the items in it changes. So even when you (think you) “add” another version of lns its items ([0] and [1]) are replaced.

// Rolf

Thanks Dale I got that working using a class. I think.
It still puzzles me tho why
xtLines.Add(lns);
doesnt do what it says on the tin. Maybe its adding a new reference rather than a new object?

It seems so, because no new array object was created. This indicates that Ins[0] and Ins[1] are actually the same memory locations on both assignments. Therefore, creating a new instance of the Ins array (on line 65) solves the problem:
bild

then all unique Lines are in the list:
bild
// Rolf

Hello I just managed to take a look at this last bit.

I was able to pretty well finish off a project I had with the help I have been given.

Id really like to take the opportunity of thanking everyone. This really is a first-class forum.
Id like to be good enough to contribute but cant see it ftm.

Help for C# scripting is not great. I find myself constantly gleaning bits and pieces from the various sources recommended and otherwise .(I also really miss being able to view local windows and single step as per Vis Studio.)

Ill be back but for now Thanks a Lot!