# C# pointsGrid

**URL:** <https://discourse.mcneel.com/t/c-pointsgrid/104076>\
**Category:** Grasshopper Developer\
**Tags:** csharp\
**Created:** [June 15, 2020, 9:19am UTC](https://discourse.mcneel.com/t/c-pointsgrid/104076 "2020-06-15T09:19:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![YellowLion](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/yellowlion/32/495060_2.png) [@YellowLion](https://discourse.mcneel.com/u/YellowLion)\
**Post date:** [June 15, 2020, 9:19am UTC](https://discourse.mcneel.com/t/c-pointsgrid/104076/1 "2020-06-15T09:19:47Z")

</div>

Line 58. I am trying to learn C#. I looked some things up. I am trying to get a grid of points.  
What I am missing?

[pointGrid00.gh](https://discourse.mcneel.com/uploads/short-url/7eXwBKvRnmlXkDTbSZi7KVxzY67.gh) (10.8 KB)

 ![pointGrid00](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/e/d/ed30fbc2008264ef687f27857899fbaa05816083.jpeg)

[https://www.grasshopper3d.com/m/discussion?id=2985220%3ATopic%3A180030](https://www.grasshopper3d.com/m/discussion?id=2985220%3ATopic%3A180030)

---

<div class="post-metadata">

**Author:** ![Mahdiyar](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mahdiyar/32/472862_2.png) [@Mahdiyar](https://discourse.mcneel.com/u/Mahdiyar)\
**Post date:** [June 15, 2020, 10:01am UTC](https://discourse.mcneel.com/t/c-pointsgrid/104076/2 "2020-06-15T10:01:10Z")

</div>

```cs
private void RunScript(Plane plane, double s, int Ex, int Ey, ref object P)
{
  List<Point3d> points = new List<Point3d>();
  Vector3d x = plane.XAxis * s;
  Vector3d y = plane.YAxis * s;
  Point3d origin = plane.Origin;
  for (int i = 0; i < Ex; i++)
  {
    for (int j = 0; j < Ey; j++)
    {
      points.Add(origin + (x * i + y * j));
    }
  }
  P = points;
}

```

[Grid.gh](https://discourse.mcneel.com/uploads/short-url/bduA7m1MsxJjmWjxhqUVo8JrbRA.gh) (3.6 KB)

---

<div class="post-metadata">

**Author:** ![YellowLion](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/yellowlion/32/495060_2.png) [@YellowLion](https://discourse.mcneel.com/u/YellowLion)\
**Post date:** [June 17, 2020, 3:58am UTC](https://discourse.mcneel.com/t/c-pointsgrid/104076/3 "2020-06-17T03:58:58Z")

</div>

Do you might know what is wrong is my code?

```
Vector3d x_dir = new Vector3d(Plane.XAxis);
x_dir = x_dir * Dis;
Vector3d y_dir = new Vector3d(Plane.YAxis);
y_dir = y_dir * Dis;
Point3d Base = new Point3d(Plane.Origin);
int i = 0;
int j = 0;
for (i = 0; i <= XNum; i++){
  for (j = 0; j <= YNum; j++){
    Point3d newPt = new Point3d(Base);
    Vector3d V1 = new Vector3d(x_dir);
    V1 = V1 * i;
    Vector3d V2 = new Vector3d(y_dir);
    V2 = V2 * j;
    newPt = newPt + V1 + V2;
    Points.Add(newPt);
  }
}
```

---

<div class="post-metadata">

**Author:** ![Mahdiyar](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mahdiyar/32/472862_2.png) [@Mahdiyar](https://discourse.mcneel.com/u/Mahdiyar)\
**Post date:** [June 17, 2020, 4:57am UTC](https://discourse.mcneel.com/t/c-pointsgrid/104076/4 "2020-06-17T04:57:59Z")

</div>

> [@YellowLion](#):
>
> what is wrong is my code?

Nothing.

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/d/8/d8c62e517d20685ffc6179de6caaf531c0463ad9.png)  
[Points.gh](https://discourse.mcneel.com/uploads/short-url/yUfbiizqppf3j44YWhhxuLJxI0.gh) (7.1 KB)

---

<div class="post-metadata">

**Author:** ![TomTom](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/tomtom/32/651939_2.png) [@TomTom](https://discourse.mcneel.com/u/TomTom)\
**Post date:** [June 17, 2020, 5:28am UTC](https://discourse.mcneel.com/t/c-pointsgrid/104076/5 "2020-06-17T05:28:28Z")

</div>

Regarding the error message, I cannot open Rhino right now, usually the error message is telling you the exact reason. I might look at it later or you post it here, but I cant spot it from the code. Looks good so far!

What @Mahdiyar did differently is a syntax improvement. Since Vector3d and Point3d are structs and no classes, whenever you get an instance, a struct will always be copied. Therefore you don‘t need to use the new keyword, using the copy constructor. If you don‘t know if its a struct or not, and the object has a copy constructor, then your approach is always the clearest way of indicating duplication.

The other improvement is that loop iteration variables (i and j) can be initialized within the loop definition. You don‘t need to do it, but it shortens code. if you write „for“ and press tab, it will autocreate this „for“ loop (its a code snippet).

I propose to stick to C# naming conventions, local variables and field variables written camelCase and public fields, properties, methods, events, delegates and classes in PascalCase. I do diverge a bit by writing private fields \_camelCase. This way its obvious what is what.

---

<div class="post-metadata">

**Author:** ![YellowLion](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/yellowlion/32/495060_2.png) [@YellowLion](https://discourse.mcneel.com/u/YellowLion)\
**Post date:** [September 8, 2020, 10:54am UTC](https://discourse.mcneel.com/t/c-pointsgrid/104076/6 "2020-09-08T10:54:20Z")

</div>

🤔 I feel quite dull when saying I cannot solve this problem. My eyes are now diverging (for reference see profile picture).  
😅 Please, can someone tell me why it does not except the Point3d? I also did _Point3d newPn (0,0,0)_ but that does not work either. Or Rhino.Geometry.Point3d, etc.  
😶 And I went through several tutorials, I can make swarms too.

```
List<Point3d> pnts = new List<Point3d>();
for(int i = 0; i < x;i++){
  for(int j = 0; j < y;j++){
    pnts.Add(Point3d(i, j, 0));
  }
}
A = pnts;

```

[question3.gh](https://discourse.mcneel.com/uploads/short-url/n9gqNbGbLXEGIbZUUjoUdj09G0e.gh) (6.1 KB)

 ![question3](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/1/9/1933e1a4b4afef1c9e76cf33eb81a7d4f3727439.jpeg)

---

<div class="post-metadata">

**Author:** ![danielbent](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/danielbent/32/631123_2.png) [@danielbent](https://discourse.mcneel.com/u/danielbent)\
**Post date:** [September 8, 2020, 11:16am UTC](https://discourse.mcneel.com/t/c-pointsgrid/104076/7 "2020-09-08T11:16:48Z")

</div>

Type  
`pnts.Add(new Point3d(i, j, 0))`  
instead of  
`pnts.Add(Point3d(i, j, 0))`  
since you construct a **new** point.
