Scripting C# Index problem. returns error: Index was out of range

Hi All,

I have a list of Boolean items that correlate with lists of x, y, and z values. I try to reference the index of one of the list of points that I am creating and it produces an index range error.

I’m open to anything that you all may think will help!

Here is the .gh link:

  1. You have not assigned list access to your inputs and this causes the component to run once for each element in the inputs list (and that’s why the error is repeated so many times in the out output), and should be executed only once if you intend to create a list with elements derived from your inputs/lists. Because of this, your component executes all the code for each input item, then you are creating a list for each input item with just one element (since for the next item will be other instance of pts list), so if you ask index 1 it gives you out of the range, because there is only one element (with index 0). To solve it, set list access to the inputs and uses a loop to iterate over each element. Right click on input > List Access.
  2. You can get more debugging information if you put your code inside the try catch statement.
  3. It is more work for the developer (you) if you establish your inputs with the three coordinates of a point instead of directly giving the point.
  4. I do not see the need to use a temporary variable for a Point (type of value/structure, that you do not modify at any time), at least you have not justified it in your description.
private void RunScript(List<Point3d> Pts, List<bool> Patt, ref object A)

List<Point3d> ptsList = new List<Point3d>();
for(int i = 0; i < Patt.Count; i++){
    //We assume that the list of points will always have the same size as the list with the Boolean pattern.
    //This will fail if Patt[0] == false
    //Maybe you want ptsList.First() or ptsList.Last() but anyway
    //add a conditional
    if(ptsList.Count > 1)
A = ptsList;

Thank you so much! This was exactly what I needed. I think It’ll work
perfectly for what I’m trying to do

What’s up with the spam?

@DavidRutten Btw. how to use try catch in GH i have index out of range ex but using standard seems not working? Error is on line 0 so its bit hard to find out whats going on…

catch (System.IndexOutOfRangeException ex)
System.ArgumentException argEx = new System.ArgumentException(“Index is out of range”, “index”, ex);
throw argEx;

If you just need to debug:

  //Your code goes here.
}catch(Exception e){
  //or RhinoApp.WriteLine(e.ToString());