Initialize Curve variable C# in Grasshopper

Dear forum,
I have a problem that might be very trivial to some of you. I am about writing a Grasshopper component and in the same fashion I would declare a Plane or a Line (like so)

var testLine = new Line();
DA.GetData(0, ref testLine);

I am now trying to declare a Curve. That is however not working, because

‘Curve’ does not contain a constructor that takes 0 arguments

However looking at the RhinoCommon API Line doesn’t either. Can someome please explain to me why a Line can be declared like that and a Curve not, and also tell me how a Curve should be declared properly? That would be very helpful, thanks!

Best,
david

Curve builders are static functions inside the Curve class, so if you type:

var testCurve = Rhino.Geometry.Curve.

it will pop up several CreateFrom… kind of functions.
https://developer.rhino3d.com/api/rhinocommon/rhino.geometry.curve/createinterpolatedcurve

@Dani_Abalde thanks for your quick reply.

But I want to create an empty container to which I can assign the curve coming from the component input.
(See my edited initial post)

This is used to retrieve Data, when you have a compiled component. So you normally feed the curve into the curve input.
Otherwise just create a line or list or whatever collection of lines and add it to.
Or do I completly miss the point of what you are trying to do?

Ahh , do you want to use

Line line = Line.Unset;

I am confused…

Curve c = null;

Before a GetData(0, ref c) should work.

1 Like

Oops. Yeah curve not line, hehe.
Guess I should read more carefully

Thank you @magicteddy, that did the trick

The solution provided will still cause an “object reference not set to an instance of an object” error while executing the script. I’m not sure what is the formal way to initialize a curve, but this one seems generic to me.
image

You don’t need to initialize any geometry, when you assign an instance over the DA object. The accepted solution is correct, but misleading and redundant. Curve crv; (declaration) is sufficient. Edit: The accepted solution is 100% correct

Note that not all curves-alike objects are Curve class instances. A curve is a base class to most, but not all curve objects. What you describe is a null reference error, you always need to check if after an assignment, the instance is not null. Input can be null in GH

And regarding initialization. In general a constructor is sometimes not sufficient to initialize a class correctly. In programming the solution to a complex or manifold initialization is the factory pattern. The lightweight solution are factory methods, static „CreateXY“ methods which offer different ways of initialization. Note that inheritance makes it even harder to understand, because you can init a Curve by calling a NurbsCurve factory method.

Curve crv; will cause a problem in visual studio.

The funny thing is, Curve crv = null; works in debugging when I tried again this time.
And I don’t know why.

Oh sorry, I should have tested. I mixed the ‘out’ with the ‘ref’ keyword. For the out keyword declaration is sufficient. But again, the error you received is a null error. This can be anywhere. You need to carefully check where it throws. I think with the debugger you should see. I think GetData also assigns a null, it will only fail if it cannot handle the input correctly, e.g. if you pass in a float instead.

C# compiler requires local variable assignment before use, and it can not garantee that GetData is going to assign the variable. Its a way to force predictability and avoid unexpected behaviour.