错误:未处理索引超出范围的异常,并且Operator'*';不能应用于类型为';对象';和

本文关键字:不能 应用于 类型 对象 Operator 索引 未处理 范围 并且 异常 错误 | 更新日期: 2023-09-27 17:59:39

我在表单1中得到了"索引超出范围异常未处理"。我将通过注释指出它。我只向您显示代码中使用以下声明的部分。

这是我的表格1

    Int32 u, v;
    int32[] l = new int32[2];
    int32[] m = new int32[2];
    int32[] g = new int32[2];
    int32[] h = new int32[2];
    int TwoClicks = 0;
    private WindowsFormsApplication2.Form2 _form2 = new WindowsFormsApplication2.Form2();
    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (TwoClicks < 2)
        {
             TwoClicks++;
        DialogResult dialogresult = _form2.ShowDialog(this);

        if (dialogresult == DialogResult.OK)
        {
                rect.Width = 0;
                rect.Height = 0;
                pictureBox1.Invalidate();
                l[TwoClicks] = e.X;// I am getting the exception here
                m[TwoClicks] = e.Y;

        }
        if (dialogresult == DialogResult.Cancel)
        {
            rect.Width = 0;
            rect.Height = 0;
            pictureBox1.Invalidate();
            TwoClicks--;
        }

        }
    }

    private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        int radius = 10; //Set the number of pixel you want to use here
        //Calculate the numbers based on radius
        int x0 = Math.Max(e.X - (radius / 2), 0),
            y0 = Math.Max(e.Y - (radius / 2), 0),
            x1 = Math.Min(e.X + (radius / 2), pictureBox1.Width),
            y1 = Math.Min(e.Y + (radius / 2), pictureBox1.Height);
        Bitmap bm = pictureBox1.Image as Bitmap; //Get the bitmap (assuming it is stored that way)
        for (int ix = x0; ix < x1; ix++)
        {
            for (int iy = y0; iy < y1; iy++)
            {
                bm.SetPixel(ix, iy, Color.Black); //Change the pixel color, maybe should be relative to bitmap
            }
        }
        pictureBox1.Refresh(); //Force refresh
            u = (e.X - l[0]) * (g[1] - g[0]) / (l[1] - l[0]);
            v = (e.Y - m[0]) * (h[1] - h[0]) / (m[1] - m[0]);
        MessageBox.Show(String.Format("latitude is {0} degrees {1} minutes and longitude is {2} degrees {3} minutes", u / 60, u % 60, v / 60, v % 60));
    }

我收到以下错误:运算符"*"不能应用于我的form2中类型为"object"answers"int"的操作数,我将其用作自定义消息框。我也很少犯其他错误。

--->无法将类型"int"隐式转换为"WindowsFormsApplication2.int32"

--->运算符"-"不能应用于类型为"WindowsFormsApplication2.int32"answers"WindowsFormsApplication2.int 32"的操作数

--->运算符"-"不能应用于"int"类型和WindowsFormsApplication2.int32"的操作数

这是我的Form2

    int32[] g = new int32[3];
    int32[] h = new int32[3];
    int TwoClicks = 0;
    private void Form2_Load(object sender, EventArgs e)
    {
            g[TwoClicks] = (int32.Parse(textBox1.Text) * 60 + int32.Parse(textBox2.Text));
            h[TwoClicks] = (int32.Parse(textBox3.Text) * 60 + int32.Parse(textBox4.Text));
    }

错误:未处理索引超出范围的异常,并且Operator'*';不能应用于类型为';对象';和

  1. 您的int[]大小为2,因此最高索引为1(基于0的索引)。不过,您正在将TwoClicks递增为2
  2. 正确的名称是Int32,而不是int32

数组具有基于0的索引。检查TwoClicks变量的值是否为2或更大,这将导致异常

int32[] l = new int32[2];
int32[] m = new int32[2];
int32[] g = new int32[2];
int32[] h = new int32[2];

我猜int32是某种自定义类。由于没有内置关键字"int32"。数学运算必须使用int或Int32。否则,框架不会自动为运算符进行强制转换。