简体   繁体   English

c#索引超出了数组的范围

[英]c# Index was outside the bounds of the array

Hello i'm getting "Index was outside the bounds of the array" if i'm putting value (i) in itemsWS.getItemInfo(itemLis[i].ToString()) 您好,如果我将itemWS.getItemInfo(itemLis [i] .ToString())中的值(i)放入,我将收到“索引超出数组范围”

    object[] itemDetails;
    object[] itemLis = itemsWS.searchItem("", "bread", "all");
    int xs = 35;
    int clefts = 0;

    for (int i = 0; i < itemLis.Length; i++)

    {
        itemDetails = itemsWS.getItemInfo(itemLis[i].ToString());
        Button myButtons = new Button();
        myButtons.Click += delegate
        {

            dataGridView1.ColumnCount = 11;

            dataGridView1.Columns[0].Name = "Item Code";
            dataGridView1.Columns[1].Name = "Description";
            dataGridView1.Columns[2].Name = "Sale Price";
            dataGridView1.Columns[3].Name = "Category";
            dataGridView1.Columns[4].Name = "Type";
            dataGridView1.Columns[5].Name = "Status";
            dataGridView1.Columns[6].Name = "Low Count";
            dataGridView1.Columns[7].Name = "Medium Count";
            dataGridView1.Columns[8].Name = "High Count";
            dataGridView1.Columns[9].Name = "Item Picture";
            dataGridView1.Columns[10].Name = "TEST";

            //here is where i'm getting error if i put i on the []

            itemDetail = itemsWS.getItemInfo(itemLi[i].ToString());

            //

            dataGridView1.Rows.Add(itemDetail);
            MessageBox.Show("data grid displayed!");

        };


        myButtons.Text = itemDetails[1].ToString() + "\n  " + itemDetails[2].ToString();
        myButtons.Top = cleft * 180;
        myButtons.Left = 70;
        myButtons.Location = new Point(xs, clefts);
        myButtons.Size = new Size(100, 60);
        tabPage1.Controls.Add(myButtons);
        xs += 135;

        if (xs >= 537)
        {
            xs = 35;
            clefts += 80;
        }

(this is the database) (这是数据库)

在此处输入图片说明

The problem is if I replace [i] with [0]. 问题是如果我将[i]替换为[0]。 It will only display the first value item in the database 它将仅显示数据库中的第一个值项目

(here is the GUI if i replaced [i] with [0]) Any button clicked would repeat the same output (如果我将[i]替换为[0],则为GUI)单击任何按钮将重复相同的输出

在此处输入图片说明

Please any help would be greatly appreciated. 请任何帮助将不胜感激。 I'm trying to display items from MySQL using new buttons which was generated that would be displayed on the datagridview. 我正在尝试使用生成的新按钮显示来自MySQL的项目,这些按钮将显示在datagridview上。 Thanks 谢谢

Try storing the value of i in a local variable inside the delegate example: 尝试将i的值存储在委托示例内的局部变量中:

myButtons.Click += delegate
{
  …
  int local_i = i;
  itemDetail = itemsWS.getItemInfo(itemLis[local_i].ToString());
  …
}

The value of i might be out of range by the time the delegate is actually executed. 实际执行委托时,i的值可能超出范围。

Update: Without seeing all the other code in the application it is difficult to say for sure but it could be that itemLis does not contain the item at that index anymore by the time the delegate is executing. 更新:无法看到应用程序中的所有其他代码,很难确定,但可能是委托执行时itemLis不再包含该索引处的项目。

Try using a foreach loop, this way you won't be retrieving the items from the itemLis array when the delegate is executing. 尝试使用foreach循环,这样,当委托执行时,您将不会从itemLis数组中检索项目。

foreach (var item in itemLis)
{
 itemDetails = itemsWS.getItemInfo(item);
    ....
    myButtons.Click += delegate
         {
         ...
         itemDetail = itemsWS.getItemInfo(item);
         ...    
     }
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM