C#:组件的输出参数索引[i]太高或太低

时间:2012-08-13 12:03:42

标签: c# parameters grasshopper

运行以下代码时出现以下错误消息:

  

c#:组件输出参数索引[1]太高或太低。

IGH_DataAccess已在帮助中提供迭代计数“获取当前迭代计数”。在解决方案期间第一次在组件上调用SolveInstance()函数时,迭代计数器将为零。每次后续呼叫都会增加1。使用DA.SetData(0, m_settings[0]);时,它会显示第一行。

错误消息显示在所使用的程序中,我在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是IGH_DataAccess.SetData方法的描述: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.

将代码更改为DA.SetDataList(i, m_settings[i]);时,第一行的字符会被拆分,而我希望每一行都被拆分。

我做错了什么?

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]);
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

这是通过使用以下代码解决的,用SetDatalist替换SetData没有循环和不同的分割字符串的方式:

  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);
                }  

            }