GH - C# weird behaviour - as always i missed something


Can somebody look at this file and explain me why slider for pop2d affects translation in c# component? It happens in v5 and in v6 (7.7 KB)

Edit: Nobody? @dale @pascal

its not a weird behaviour, its doing things as expected.
You move the shape x times. What are you trying to do?

“shape” is a class, so when doing var nShp = shape you just passing the reference in memory.It doesn’t copy the shape.
A Vector3d in comparison is a struct not a class, so applying the = always copies.
For duplication of classes reconstruct it or look out for a “Duplicate” or “Copy” method. Curve nshp = shape.DuplicateCurve() solves it.
It may also a good idea to never use “var” if the initialisation does not hint a type.
Read more about classes and structs in C#, its a weird and common beginner error.

@TomTom Hmm? Does it ? So this simple example works as i expect and when curve object is taken into account it uses new ref each time ? (11.0 KB)

Btw. “shape” shouldn’t be an object of class Curve?

Indeed DuplicateCurve() solves this but i still don’t understand how this is referencing class instead of object of that class :open_mouth:

As I said, I don’t know what your aim is.

int is value type, Vector3d = struct = value type , GeometryBase = class = reference type.

For GeometryBase there is also a “Duplicate” method

An object is an instance of a class. @TomTom meant to say that you are using an object of a class, not a struct.

1 Like

@nathanletwory Sure but how new item gets xform of previous ?


var nShp = shape

give second object which is the same as first one ? Even if not how shape which is outside foreach get xform of nShp ?

It goes like xform -> nShp(xform) -> shape(xform) ??? I’m probably just lame when it comes to inheritance :roll_eyes:

Hmmmm… i probably understand that normaly i would use type var = new classConstructor - but in case or existing shape i can’t use new so i have to dulplicate it

For classes:
It gives not the object, it gives the address to the object.
So it copies the address, not the object itself.
So whenever you do something with your shape you are going to the same address and look out for things you can do there.

For structs and primitive types:
It just copies the values in there.

This has nothing to do with inheritance.

You can also google for Heap and Stack memory allocation if you want to know the mechanics behind, which isn’t easy at all

1 Like

“new” creates a new instance. So by creating a new instance and filling it up with the same values you are copying. However what happends if a object owns an object and so forth. Sometimes you need to do a “deep copy”. This however is up to the person who actually codes the class. That is why McNeels offers some Duplicate methods.

1 Like

… and sometimes (in Rhino quite often) an object is created static.
Static creation doesn’t requires the new keyword

1 Like