C#: Output Parameter Index[i] too high or too low

2019-05-11 11:42发布

问题:

The following error message appears when running the code below:

c#: Output Parameter Index[1] too high or too low for Component.

The IGH_DataAccess is already providing an iteration count in the help it says "Gets the current iteration count". The first time the SolveInstance() function is called on a component during a solution the Iteration counter will be zero. It will be incremented by one for every subsequent call. When using DA.SetData(0, m_settings[0]); it does show the first line.

The error message appears in the program used and I get the following exception on DA.SetData(i, m_settings[i]);:

System.Exception occurred Message=Unknown file Source=Grasshopper StackTrace: at Grasshopper.Global_Proc.ASSERT(Guid assert_id, String message, Exception exception) in C:\dev\Grasshopper\1.0\root\src\GH_GlobalProc.vb:line 98 InnerException:

Herafter is the description of the IGH_DataAccess.SetData Method: Stores data in an output parameter during GH_Component.SolveInstance(). Use this function only for setting individual data items. If you want to set lists of data, you *must* call SetDataList() instead.

When changing the code to DA.SetDataList(i, m_settings[i]); the characters of the first line get split while I want every line to get split.

What am I doing wrong?

string[] m_settings;

public void ShowSettingsGui()
{
    var dialog = new OpenFileDialog { 
                     Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*" };
    if (dialog.ShowDialog() != DialogResult.OK) return;
    m_settings = File.ReadAllLines(dialog.FileName);
    ExpireSolution(true);
}

protected override void SolveInstance(IGH_DataAccess DA)
{
    if (m_settings == null)
    {
        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, 
                          "You must declare some valid settings");
        return;
    }

    for (var i = 0; i < m_settings.Length; i++)
    {
        DA.SetData(i, m_settings[i]);
    }
}

Thanks in advance!

回答1:

This was solved by using the following code, replacing the SetData by SetDatalist with no loop and a different way of splitting the strings:

  string m_settings_temp;
            string[] m_settings;
            public void ShowSettingsGui()
            {
                var dialog = new OpenFileDialog { Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*" };
                if (dialog.ShowDialog() != DialogResult.OK) return;

                m_settings_temp = File.ReadAllText(dialog.FileName);
                m_settings = m_settings_temp.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                ExpireSolution(true);
            }



            protected override void SolveInstance(IGH_DataAccess DA)
            {
                if (m_settings == null)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
                    return;
                }

                else

                {  
                        DA.SetDataList(0, m_settings);
                }  

            }