0

I am working on a Revit Add-in and in that add-in I am trying to use a List(Of List(Of Curve)), however I'm having issue accessing the data from the sublists.

Dim ClosedCurveList As New List(Of List(Of Curve))
Dim ClosedCurve As new List (Of Curve)

For i=0 To FinalWallLines.Count-1
    If FinalWallLines(i+1).GetEndPoint(0).X = FinalWallLines(i).GetEndPoint(1).X And _
       FinalWallLines(i+1).GetEndPoint(0).Y = FinalWallLines(i).GetEndPoint(1).Y And _
       FinalWallLines(i+1).GetEndPoint(0).Z = FinalWallLines(i).GetEndPoint(1).Z Then

        ClosedCurve.Add(FinalWallLines(i))

    Else

        TaskDialog.Show("A",ClosedCurve.Count)
        ClosedCurveList.Add(ClosedCurve)
        TaskDialog.Show("B", ClosedCurveList(ClosedCurveList.Count-1).Count)
        ClosedCurve.Clear()

    End if
Next

TaskDialog.Show("C", ClosedCurveList.Count)

For i=0 To ClosedCurveList.Count-1
    TaskDialog.Show(i,ClosedCurveList(i).Count)
next

So when I run that code, the first TaskDialog.Show("A",ClosedCurve.Count) shows me that all of ClosedCurve are made of 4 curves, which makes sense as all my curves are forming rectangles.

My second TaskDialog.Show("B", ClosedCurveList(ClosedCurveList.Count-1).Count) also return 4 as the count for each of the sublists, as expected.

My third TaskDialog.Show("C", ClosedCurveList.Count) returns 23.

So from that, we can gather than ClosedCurveList is a list of 23 lists of 4 curves.

However, during my loop For i=0 To ClosedCurveList.Count-1, my TaskDialog.Show(i,ClosedCurveList(i).Count) returns 23 0s.

Would anyone know why I am not getting 23 4s as expected when trying to access the count of each of my sublists?

RobertBaron
  • 2,817
  • 1
  • 12
  • 19

1 Answers1

1

Instead of ClosedCurve.Clear() you should have ClosedCurve = new List(Of Curve).

When you add it to ClosedCurveList you are not adding a copy. You are adding the reference to the object CLosedCurve. And so, when you clear ClosedCurve, it also clears the one which was added in ClosedCurveList because they are references to the same object. By re-assigning a new List(Of Curve) to ClosedCurve, you will now have separate references, like you were originally expecting.

tgolisch
  • 6,549
  • 3
  • 24
  • 42