C# Grasshopper RemoveAt Error

RemoveAt_Example.gh (4.7 KB)

I’m trying to learn C# for Grasshopper, and falling at one of the first hurdles. I’m hoping someone can help.

I have a list of point (input as a list of Point3d), I’m simply trying to remove the last item in the list if the list count is odd.

When I try running the code below I get the error:
Error (CS0029): Cannot implicitly convert type void to System.Collections.Generic.List<Rhino.Geometry.Point3d>

What am I doing wrong?

  private void RunScript(List<Point3d> iPts, ref object A)
  {
    int cPtsCount = iPts.Count;
    int cMod = cPtsCount % 2;
    List<Point3d> cPoints = new List<Point3d>(iPts);
    List <Point3d> oPoints = new List<Point3d>();

    if (cMod == 1) {
      oPoints = cPoints.RemoveAt(cPtsCount - 1);
    }
    else
    {
      oPoints = cPoints;
    }
    A = oPoints;
  }

That’s the problem. The RemoveAt() method does not return a new collection with the item removed, it returns nothing (i.e. void) and modifies the underlying collection instead.

I’d type it like this:

private void RunScript(List<Point3d> P, ref object A)
{
  int count = P.Count;
  if (count % 2 == 1)
    P.RemoveAt(count - 1);
  A = P;
}

Understood. Thanks for the explanation.

First forum post, and a response from the man himself. Big fan of your work, and all at McNeel. Appreciate you taking the time.

Cheers.