I’m not sure if my title is clear or specific enough but here is what I’m trying to do.
I have a new class
public class Segments
{
public List<double> List1 { get; set; }
public List<double> List2 { get; set; }
public List<double> List3 { get; set; }
public List<double> List4 { get; set; }
}
public static void SplitSegments(CsvClass longList, List<Segments> segments)
{
Segments tempSegments = new Segments();
List<double> list1 = new List<double>();
List<double> list2 = new List<double>();
List<double> list3 = new List<double>();
List<double> list4 = new List<double>();
// Nested For loop that goes through a longList with its own properties.
// Below is a flag for when to split that longList.properties into segments
if (flag == true)
{
//The lists are now complete for the first segment.
list1.Add(longList.one[i]);
list2.Add(longList.two[i]);
list3.Add(longList.three[i]);
list4.Add(longList.four[i]);
//created a copy of the class properties
tempSegments.List1 = new List<double>(list1);
tempSegments.List2 = new List<double>(list2);
tempSegments.List3 = new List<double>(list3);
tempSegments.List4 = new List<double>(list4);
//Add to List<Segments>
segments.Add(tempSegments)
//Clear lists in order to move on to creating next segment of the longList.
list1.Clear();
list2.Clear();
list3.Clear();
list4.Clear();
break;
}
}
}
My problem is when the new segment is created and added to the List<Segments>, all the segments become the same exact copy of the new segment.
I suppose the Lists in the class still reference the same objects as the lists. My question is how do I make it so that when the new segments are added to the List<Segments>, they don’t erase the old segments?
You should move the line
into the body of the loop, probably right within the
ifclauseif (flag == true). This would create a new segment on each iteration.The whole part of code would look like: