Grasshopper

algorithmic modeling for Rhino

Custom Class from one component to another (GH_ObjectWrapper)

I'm trying to send an object from one component to another, in the easiest way possible.

 

the class:

 

    public class SonicStep
        {
            private string distance;


            public SonicStep(string legdistance)
            {

                this.distance = legdistance;

            }

            public string Distance
            {
                get { return distance; }
                set { distance = value; }
            }
        }

 

setup:

 

 pManager.Register_GenericParam("SonicStep", "Steps", "Each Step from A to B (Use the Step Component to read this output)");

 

SonicStep stepTemp = new SonicStep("some distance");

DA.SetData(7, new GH_ObjectWrapper(stepTemp));

 

 

Then bringing it in on the other component:

 

GH_ObjectWrapper allSteps = new GH_ObjectWrapper();

if (!DA.GetData<GH_ObjectWrapper>(0, ref allSteps)) {return;}

 

SonicStep tempStep = allSteps.Value as SonicStep;

string tempdist = tempStep.Distance;

 

 

I get an error that says "Object reference not set to an object......etc etc"

 

Any ideas?

 

 

 

Views: 793

Replies to This Discussion

Did you forget this?

if (!DA.GetData<GH_ObjectWrapper>(0, ref allSteps))

  return;

Hi Sridevi,

I was using ....................if (!DA.GetData(0, ref allSteps)).....

tried if (!DA.GetData(0, ref allSteps)) aswell, no go :(

Any ideas?

Since the "as" keyword will set your SonicStep to null if the cast doesn't work, it is probably a good idea to test for null before trying to access the properties of the object. 

however otherwise your code works for me... the only other thing I can think of - is 7 the correct index for the SetData? I usually do this by name rather than index to avoid confusion.

Very odd, do you have your version of the code? I'll compare the two.

Thanks Andrew
Actually, it might be because the two components are in different assemblies????

They have the same class "sonic step" though?

ahhhh yes. That would be the problem. Haven't tested this, but you could probably include a reference to the other assembly? or define the class in a .dll external to the two GHAs?

Update - I played around and got this to work. An external dll containing only the SonicStep class, and two GHAs containing components which output and receive the custom type respectively. However - I ran into the problem described here - the external dll reference only worked when COFF loading was disabled. 

I know there are GHA libraries (sawapan, kangaroo, etc) that refer to external dlls and function properly even with COFF loading enabled. So there's gotta be a way...

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service