Grasshopper

algorithmic modeling for Rhino

Very urgent!!!! how to solve the index out of range problem?

i am a new beginner to use the code?
so , How can i start the code?

just like this code! is this any other method or better code to solve this problem?

1. Solution exception:Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

List<Point3d> pList = new List<Point3d>(){p1,p2,p3,p4,p5,p6};

for(int i = 0, i<pList.Count;i++){

   Point3d pCurrent = pList[i];

   Point3d pNext;

    try{pNext = pList[i+1];} catch{pNext = pList[0];} // meaning the obvious, he he

   Line line = new Line(pCurrent, pNext);

   ... blah, blah ...

}

List<Point3d> pList = new List<Point3d>(){p1,p2,p3,p4,p5,p6};

for(int i = 0, i<pList.Count;i++){

   Point3d pCurrent = pList[i];

   Point3d pNext;

    try{pNext = pList[i+1];} catch{pNext = pList[0];} // meaning the obvious, he he

   Line line = new Line(pCurrent, pNext);

   ... blah, blah ...

}

Views: 234

Replies to This Discussion

You can use an if statement to check if you are dealing with the last element of the list

Another way would be:

List<Point3d> pList = new List<Point3d>(){p1, p2, p3, p4, p5, p6};

pList.Add(pList[0]);

for(int i = 0, i < pList.Count - 1; i++){

   Point3d pCurrent = pList[i];

   Point3d pNext = pList[i+1];

   Line line = new Line(pCurrent, pNext);

   ... blah, blah ...

}

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service