gpt4 book ai didi

c# - 错误 : Index out of range exception unhandled and Operator '*' cannot be applied to operands of type 'object' and 'int'

转载 作者:太空宇宙 更新时间:2023-11-03 20:33:11 25 4
gpt4 key购买 nike

我的 form1 中出现“未处理的索引超出范围异常”。我将通过评论指出它。我只向您展示我使用以下声明的代码部分。

这是我的表格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”和“int”的操作数。我也遇到了一些其他错误。

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

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

--->运算符'-'不能应用于类型'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));

}

最佳答案

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

关于c# - 错误 : Index out of range exception unhandled and Operator '*' cannot be applied to operands of type 'object' and 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6401757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com