Sphere method in C#

Hi, I just started learning C# coding and I am trying to define a method that will create and grid of spheres but I get this error at the end:

Error (CS0029): Cannot implicitly convert type ‘Rhino.Geometry.Sphere’ to ‘System.Collections.Generic.List<Rhino.Geometry.Sphere>’ (line 77)

  private     List<Sphere> spheresList (int x, int y, double r)
  {
    int rows = 0;
    int columns = 0;
    double radius = 0;
    for (int i = 0;i < 200;  i = i + rows){
      for (int j = 0;j < 350; j = j + columns){
        Point3d myPoint = new Point3d(i, j, 0);
        Sphere mySphere = new Sphere(myPoint, radius);
        //spheresList.Add(mySphere);

        return mySphere;
      }
    }
  }

So should I convert the type? If so, how? I attach the gh file so you can see the error.

methodSphere.gh (6.5 KB)

Your method spheresList expects a List<Sphere to be returned, but you return mySphere.
Also, your commented row doesn’t work, because you did not define spheresList inside the method as on object (a new List)
Also, you return it inside the for loop, which will basically exit the method after one run.
Also, you didn’t use your x, y and r arguments.
Also, maybe you should also add integer inputs to define the number of rows and columns, instead of having that 200 and 350 values inside the generic method ?

Keep the effort learning C# ! :smiley:

methodSphere.gh (12.5 KB)

private void RunScript(int x, int y, int r, ref object A)
  {
    //Sphere sphere = new Sphere (pt, r);

    List<Sphere> spheres = spheresList(x, y, r);

    A = spheres;

  }
private List<Sphere> spheresList (int x, int y, double r)
  {
    List<Sphere> s = new List<Sphere>();

    for (int i = 0;i < 200;  i = i + x)
    {
      for (int j = 0;j < 350; j = j + y)
      {
        Point3d myPoint = new Point3d(i, j, 0);
        Sphere mySphere = new Sphere(myPoint, r);
        s.Add(mySphere);
      }
    }
    return s;
  }

Thanks! Understood, really sill* beginner mistakes x)

A last question: if I use the method twice in order to obtain 2 list of spheres, how could I merge them in a single one? My first guess is to Add one list to the other, but something is not correct (Error (CS1502): The best overloaded method match for ‘System.Collections.Generic.List<Rhino.Geometry.Sphere>.Add(Rhino.Geometry.Sphere)’ has some invalid arguments (line 59)
Error (CS1503): Argument 1: cannot convert from ‘System.Collections.Generic.List<Rhino.Geometry.Sphere>’ to ‘Rhino.Geometry.Sphere’ (line 59)
):

    List<Sphere> s1 = spheresList(x, y, r);
    List<Sphere> s2 = spheresList(x + 4.5, y - 2.5, r - 3.5);
    s1.Add(s2);

    A = s1;
 

  }

  // <Custom additional code> 
  private     List<Sphere> spheresList (double x, double y, double r){
    List <Sphere> s = new List<Sphere>();
    for (double i = 0;i < 200;  i = i + x){
      for (double j = 0;j < 350; j = j + y){
        Point3d myPoint = new Point3d(i, j, 0);
        Sphere mySphere = new Sphere(myPoint, r);
        s.Add(mySphere);
      }
    }
    return s;
  }

s1.AddRange(s2)

Thanks again. I am now dealing a bit with trees. I managed to create a tree of spheres with a constant radius. I would like however to insert a tree of radius so each list takes one different radius. I am using foreach loops but I am not capable to introduce the variable of radius. Any hint ? Thanks!

  {
    DataTree <Sphere> spheresTree = new DataTree<Sphere>();
    int pathIndex = 0;

  /*foreach (List <double> rList in rTree.Branches{
      List <double> radiusList = new List <double>();*/


    foreach (List <Point3d> ptList in ptTree.Branches){
      List <Sphere> spheresList = new List<Sphere>();
      GH_Path path = new GH_Path(pathIndex);
      pathIndex = pathIndex + 1;
      foreach (Point3d pt in ptList){
        Sphere mySphere = new Sphere (pt, 5);
        spheresList.Add(mySphere);
      }
      spheresTree.AddRange(spheresList, path);
    }

    A = spheresTree;

Hello,

Assuming your radiiTree has at least as many branches as ptTree, you can access the i-th branch of the radii by the same index as your current branch of spheresTree, which is pathIndex.

There is an .Add method for DataTrees that accepts some data and a GH_Path, which avoids dealing with the temporary List<Sphere>.

Also : usually, the data structure management is left to Grasshopper. Right now your component does exactly the same thing as a simple Sphere component which is fed trees of points and radii. Are you on the first steps to something more complicated that indeed requires the inputs as tree, or is it only for learning purposes ?

private void RunScript(DataTree<Point3d> ptTree, DataTree<double> radii, ref object A)
  {
    DataTree <Sphere> spheresTree = new DataTree<Sphere>();
    int pathIndex = 0;

    foreach (List <Point3d> ptList in ptTree.Branches)
    {
      //List <Sphere> spheresList = new List<Sphere>();
      GH_Path path = new GH_Path(pathIndex);

      List<double> rad = radii.Branches[pathIndex];
      double radius = rad[0];

      foreach (Point3d pt in ptList)
      {
        Sphere mySphere = new Sphere (pt, radius);
        //spheresList.Add(mySphere);
        spheresTree.Add(mySphere, path);
      }
      //spheresTree.AddRange(spheresList, path);

      pathIndex++;
    }

    A = spheresTree;
  }

SphereC#Trees.gh (5.7 KB)