Grasshopper

algorithmic modeling for Rhino

I have 2 custom parameters handling 2 custom classes.

The first class, lets call it MotherClass stores the other one inside (let's call the other one ChildClass).

1st component creates ChildClass and outputs it.

2nd component gets the ChildClass and packs it inside MotherClass. Then it outputs MotherClass to some other component which reads properties of MotherClass... and some of those properties come from ChildClass.

ChildClass fields are simple types - string, few bytes and integers.

MotherClass fields are all the fields from ChildClass and one field storing a Polyline.

Both classes of course Inherit Grasshopper.Kernel.Types.GH_Goo(of T)

The question are :

  • should I Implement IDisposable interface in any of those classes. If so, which fields must be cleaned ?
  • should I pass those classes to other parameters by DA(0, MotherClass.Duplicate?) or it is already there by GH_Goo ?
  • should I create ChildClass and MotherClass in SolveInstance, or  create it once as a component's field and then change theirs properties and pass it to DA (as duplicate ?).... 
  • if I create those classes in SolveInstance, is it necessary to Dispose them there ?
  • finally - maybe it would be better if MotherClass inherits the ChildClass ?

Thanks in advance and Happy New Year.

Views: 362

Replies to This Discussion

1) should I Implement IDisposable interface in any of those classes. If so, which fields must be cleaned?

No. Primitive .NET types need not be disposed. Polyline can also be left to the garbage collector. I do not dispose of any IGH_Goo instances, mostly because I have no idea when an instance is truly no longer needed. If any of your fields need to be disposed, you may have to implement a destructor, but I have no experience with this.

2) should I pass those classes to other parameters by DA(0, MotherClass.Duplicate?) or it is already there by GH_Goo ?

IGH_Goo is not duplicated by default. If you use DA.GetData() and ask for IGH_Goo types, you'll get a reference to the same instance as exists. Thus, if you take in an instance of your type, modify and output it, you should duplicate it yourself. But you only need to do this if you change the state of an instance.

MyGooType data = null;

if (!DA.GetData(0, ref data)) return;

data = data.Duplicate() as MyGooType;

data.Property = newValue;

DA.SetData(0, data);

3) should I create ChildClass and MotherClass in SolveInstance, or  create it once as a component's field and then change theirs properties and pass it to DA (as duplicate ?).... 

It's almost always better to use variables with the lowest possible scope. So method variables are preferred to class variables, class variables are preferred to static variables.

4) if I create those classes in SolveInstance, is it necessary to Dispose them there ?

NO! Do not dispose of instances that are passed on to output parameters. Disposing objects typically makes them invalid, so if you share instances with anyone else, you should not dispose them or the other code may well crash. However I don't think your types need to be disposable so this is a moot point now.

In general, if you're dealing with disposable types, and the instances aren't shared, then you dispose them as quickly as possible. But if they are shared it's a lot more complicated.

5) finally - maybe it would be better if MotherClass inherits the ChildClass ?

Maybe. Not necessarily. Depends on the classes. 

Thank you, that really explains a lot.

I think I got a bit confused when I saw that by inheriting GH_Goo I have to implement the Duplicate function (at least it appears there)... hence the question about duplicating.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service