【发布时间】:2020-02-29 14:16:40
【问题描述】:
由于某种原因,当我将 int 大小和板数组 int 放入公共类时,它给了我 2 个错误: 第一个是:
字段初始值设定项不能引用非静态字段、方法或属性“Form1.textBox1”
第二个:
字段初始值设定项不能引用非静态字段、方法或属性“Form1.size”
public partial class Form1 : Form
{
int size = int.Parse(Textbox1.Text)
Button[,] board = new Button[size,size];
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
random code that needs the board array
}
private void Form1_Click(object sender, EventArgs e)
{
other random code that need the board array
}
【问题讨论】:
-
为什么表单一初始化就解析文本框的文本?用户甚至没有机会在文本框中输入任何内容!
-
当我把它放在其他地方时,这个错误仍然发生“字段初始化程序无法引用非静态字段、方法或属性'Form1.size'”