Retrieving data list from variable parameters

Hi guys,

I am trying to retreive a List of data through all my variable parameters which have their Access set to GH_ParamAccess.list.

All variable parameters are of type Param_GenericObject because they will receive as input any type of data ( meshes, strings, points, vectors…etc)

I have been sucessfull in the past on retreiving data from these variable parameters when their Access is set to GH_ParamAccess.item. But now I am having some trouble when they are set to List

I have ran through the code in debug mode and I always have null values, after I call DA.GetDataList() on the corresponding variable parameter.

I hope that some code and these two images can help to illustrate my point more explicitly.

Image 1: variable component highlighted in blue and a List<Point3d> as input

Image 2: Showing null values in VS after calling DA.GetDataList()

Code from Solve Instance ( just to give some backgorund):

protected override void SolveInstance(IGH_DataAccess DA)
        {

          // Gather data from normal inputs
            bool _send = false;
            string _id = "";

            DA.GetData(0, ref _send);
            DA.GetData(1, ref _id);

          // Start gathering data from variable parameters
            int threshold = 0;
            int inputCount = 0;

            for (int i = 0; i < Params.Input.Count; i++)
            {
                if (Params.Input[i].Name != "Data") threshold++;
                if (Params.Input[i].Name == "Data") inputCount++;
            }

            List<int> indexes = new List<int>();
            DataContainer dc = new DataContainer(inputCount);
        
            List<DataContainerFactory> _dataContainers = new List<DataContainerFactory>();

            // Iterate through params and find ZUI parameters (if there are any)
            for (int i = 0; i < Params.Input.Count; i++)
            {
                if (i > threshold - 1)
                {
                    // Set temp data
                    int count = Params.Input[i].VolatileDataCount;
                    IParaData[] s = new IParaData [count];
                    _dataContainers.Add(new DataContainerFactory(s));
                    indexes.Add(i);
                }
            }


            // Retrieve data from ZUI parameters ( if there are any)
            // and assign it to _dataContainers
            for (int i = 0; i < _dataContainers.Count; i++)
            {  
               // Here is were the data should actually get collected... but it does not
                DA.GetDataList(indexes[i],  _dataContainers[i].data.ToList());
            }
        

            ///*** ALL ITEMS IN DATA CONTAINERS ARE ALWAYS NULL EVEN THOUGH DA.GETDATALIST 
            ///*** WAS CALLED ON EVERY VARIABLE PARAMETER AND COLLECTED THE DATA

            for (int i = 0; i < _dataContainers.Count; i++)
            {
                IParaData[] pp = new IParaData[_dataContainers[i].data.Length]; 
                for (int j = 0; j < _dataContainers[i].data.Length; j++)
                {

                    // for such reason this condition in particular is not getting executed
                    // because a Point3d is obviously not a null. But the same would hold true
                    // if it were a Polyline or any other type.

                    if (_dataContainers[i].data[j] is Point3d p)
                    {                    
                       pp[j] = new Para_Point3d(p.X, p.Y, p.Z);                       
                    }

                    else if(_dataContainers[i].data[j] is Polyline)
                    {
                        pp[j] = new Para_Polyline<Polyline>(_dataContainers[i].data[j] as Polyline);
                    }

                    /// So on and so forth....
                }
                   
                dc.Container[i] = pp;
                
            }

        }

PS: sorry for the messy code, I am putting together some stuff rather quickly.

Any ideas would be very helpful !

The problem is here:

DA.GetDataList(indexes[i],  _dataContainers[i].data.ToList());

I don’t know about the structure of your class, but the ToList() creates a (temporary) copy of the data IEnumerable. The data is retrieved and stored in that copy (which then is collected by the GC i guess, because it isn’t used further). Set up a temporary List<> variable, collect the data, then copy or assign the data to your custom class.

1 Like

As @dsonntag suggest, but for an arbitrary number of inputs (as seems it is your case due to ZUI?):

List<object>[] dataListArray = new List<object>[_dataContainers.Count];
for (int i = 0; i < _dataContainers.Count; i++)
{        
   DA.GetDataList(indexes[i],  dataListArray[i]);
} 
// Now dataListArray has input data. An array of list of objects, where each position is an input an each list is the datalist of that input.
1 Like

@dsonntag yes! you are right in pointing that out! I missed that obvious detail there. ToList creates a copy and thats why I wasn’t working anymore on the original data.