C# Grasshopper Component inheritance

Hi,

I have basic question about class inheritance.

I have created Grasshopper component base class, not to write the same things for each component:

When I inherit this class in another component, like this below and I have two the same components on grasshopper canvas. One of them will overwrite/change variables such as preview, bbox, m, lines, directory or other variables from base class. Will it only be affected/changed for one component only while other will have initial values?

That should work fine. This is what I use for GhShaderNodes as well, where I actually have a chain of base classes:

I have a CyclesNode class deriving from GH_Component. This is where I essentially dynamically create the inputs and outputs based on the types in ccl.ShaderNodes.* and some extra stuff for the heavy lifting. Then for the math nodes I have yet another base class MathBaseNode deriving from CyclesNode.

This all works fine.

Thank you just wanted to be sure before inheriting in all components:)

To make a GH_Component base class, declare it as abstract to force inheritors to overwrite methods that it does not overwrite, such as Guid, It’s just good practice I think.

Regarding your question, it depends on the implementation. If the fields are not declared static, in principle they should not share the values.

Hi @Petras_Vestartas,

Here is a sample that demonstrates GH_Component inheritance if you need one.

https://github.com/mcneel/rhino-developer-samples/tree/5/grasshopper/cs/SamplePlatonics

– Dale