错误1字段初始化程序无法引用非静态字段,方法或属性 [英] Error 1 A field initializer cannot reference the non-static field, method, or property
本文介绍了错误1字段初始化程序无法引用非静态字段,方法或属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public partial class Form1 : Form
{
Class1 class = new Class1(30,a);
public Form1()
{
InitializeComponent();
}
public int a = 0;
private void Timer1_Tick(object sender, EventArgs e)
{
a += 1;
}
}
我想在变量中使用变量'a'卡尔斯,但我无法通过我使用的构造函数将其移动到我的班级。
我收到的错误消息是:
I want to use the variable 'a' in my calss but i cant get "move" it over to my class via the constructor i'm using. The error message i recive is :
错误:字段初始化器无法引用非静态字段,方法或属性。
Error: A field initializer cannot reference the non-static field, method, or property.
我知道这是一个基本问题,但可以得到帮助
I know it's a basic problem but help is appreciated
class Class1
{
private int r;
private int x;
public Construct(int p, int c)
{
this.r = p;
this.x = c;
}
}
推荐答案
只需将 class1
的初始化移动到构造函数中即可:
Just move the initialization of class1
into a constructor:
class Form1 {
int a = 0;
Class1 obj1;
public Form1() {
obj1 = new Class1(a);
}
}
这篇关于错误1字段初始化程序无法引用非静态字段,方法或属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文