Reading and assigning multitype Datatree input in C#

Hi all,
I am working on a custom Grasshopper component that in essence separates data into streams. I realize that there probably exist methods to accomplish what I am trying to accomplish using existing Grasshopper components, however it is the methodology (in C#) that I am trying to figure out to tailor the component to my liking.That as a premise to my question.

I am inputting a datatree with n branches that all embed multitype data. With that I mean that the tree (and each branch) both embeds string and double (or equivalent) datatypes. I have been scoundering the fora but I have not found an answer to this (probably very simple) problem I am having: how to read this sort of data and use it in the SolveInstance method.

I presume that I need to register a generic input parameter, due to the multitype data. I do this as follows:

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
    pManager.AddGenericParameter("Data", "D", "Data to convert", GH_ParamAccess.tree);
}

In the SolveInstance method, following the template file for Grasshopper components and as I have found on multiple fora and API documentation websites, I use the GH_Structure and the IGH_Goo interface (however I am not certain what an interface exactly means, but that is due to my limited knowledge of coding in C#), to assign the inputted data to a variable:

protected override void SolveInstance(IGH_DataAccess DA)
{
    // Initialize values
    GH_Structure<IGH_Goo> tree;
    if (!DA.GetDataTree(0, out tree)) return;

    //Actual method
    List<GH_Path> paths = tree.Paths.ToList();

    // Read data (only specific data per branch)
    // First the presumed string data
    List<GH_String> stringData = new List<GH_String>();
    List<IGH_Goo> stringGoo = tree.Branches[1].ToList(); // only interested in the second branch for this data
    for (int i = 8; i < treeCombs.Count; i++)
    {
        treeCombs[i].CastTo(out GH_String test);
        stringData.Add(test);
    }
    // Second the presumed double/number data
    List<GH_Number> numberData = new List<GH_Number>();
    for (int i = 8; i < tree[2].Count; i++)
    {
        tree[2][i].CastTo(out GH_Number test);
        numberData.Add(test);

    // Output data
    DA.SetDataList(0, stringData); // added as TextParameter
    DA.SetDataList(1, numberData); // added as NumberParameter
    }

Note that I have listed here two methods that I tried to implement:
1.) By preallocating a branch and then itertating over its data
2.) By directly accessing its data

These are the two main ways I thought that should be able to function properly. Instead, I am getting back null data for each i, which I can read in Grasshopper while debugging.

Also note that the paths variable is unused in this setup, as this was merely a debugging addition, which returned the correct amount of paths of the inputted data.

Obviously I am missing a (probably very simlpe) point here, as multitype data is something quite common in Grasshopper I presume.

Would someone be able to explain how to properly acces my (multitype) data and use it in the method (in a separated way), maybe elaborating a bit on the idea of the IGH_Goo interface with respect to multitype data?

Thanks in advance!

Daan

Since the multiType Tree is - obviously - of Type object you need to unbox and check on a per item basis. Practice with some test like this (BTW: I never use/work with VS - the fact that the build in editor is crap … adds to the pleasure):

That said putting all your onions in the same bag … sometimes is good (for complex data structures, using many C#'s that read your data … etc etc) but sometimes not so (cost of unboxing). That said the “trad” way is to use a Class (with the cost of unboxing always around). Anyway … if at some later time you want to read a List (due to a Class) from one C# to another … notify.

Thanks for your answer Peter. However, I am not entirely sure if your explanation is what I am looking for. Your way does not handle the registering of a tree as an input of a custom component in a plugin (indeed through VS and a custom .gha file).

Thanks anyhow!

Daan

As I stated I never use VS (nor any GH Methods except the bare minimum - for instance the DT related ones).