CS0120:非静态字段,方法或属性'foo'需要对象引用 [英] CS0120: An object reference is required for the nonstatic field, method, or property 'foo'
问题描述
考虑:
命名空间WindowsApplication1
{
公共局部类Form1:表单
{
Public Form1()
{
InitializeComponent();
}
private void button1_Click(对象发送者,EventArgs e)
{
// int [] val = {0,0};
int val;
if(textBox1.Text ==)
{
MessageBox.Show(输入任何数字);
}
else
{
val = Convert.ToInt32(textBox1.Text);
线程ot1 =新线程(新ParameterizedThreadStart(SumData));
ot1.Start(val);
}
}
私有静态无效ReadData(对象状态)
{
System.Windows.Forms.Application.Run();
}
void setTextboxText(int result)
{
if(this.InvokeRequired)
{
this.Invoke(new IntDelegate( SetTextboxTextSafe),新对象[] {result});
}
其他
{
SetTextboxTextSafe(result);
}
}
void SetTextboxTextSafe(int result)
{
label1.Text = result.ToString();
}
私有静态无效SumData(对象状态)
{
int result;
// int [] icount =(int [])state;
int icount =(int)状态;
for(int i = icount; i> 0; i--)
{
结果+ = i;
System.Threading.Thread.Sleep(1000);
}
setTextboxText(result);
}
代表无效IntDelegate(int result);
private void button2_Click(对象发送者,EventArgs e)
{
Application.Exit();
}
}
}
为什么会发生此错误?
非静态字段,方法或属性'WindowsApplication1.Form1.setTextboxText(int)
$需要对象引用b $ b
看起来您正在调用一个非静态成员(属性或方法,特别是 setTextboxText
)从静态方法(特别是 SumData
)。您将需要:
-
使被叫成员也保持静态:
静态无效setTextboxText(int结果)
{
//为setTextboxText编写静态逻辑。
//这可能需要Form1的静态单例实例。
}
-
创建
Form1的实例
在调用方法中:私有静态void SumData(对象状态)
{
int结果= 0;
// int [] icount =(int [])state;
int icount =(int)状态;
for(int i = icount; i> 0; i--)
{
结果+ = i;
System.Threading.Thread.Sleep(1000);
}
Form1 frm1 = new Form1();
frm1.setTextboxText(result);
}
在
Form1 $ c实例中传递$ p>
-
使调用方法成为非静态实例方法(
Form1 $ c) $ c>):
private void SumData(对象状态)
{
int result = 0;
// int [] icount =(int [])state;
int icount =(int)状态;
for(int i = icount; i> 0; i--)
{
结果+ = i;
System.Threading.Thread.Sleep(1000);
}
setTextboxText(result);
}
有关此信息可以在MSDN上找到错误。
Consider:
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//int[] val = { 0, 0};
int val;
if (textBox1.Text == "")
{
MessageBox.Show("Input any no");
}
else
{
val = Convert.ToInt32(textBox1.Text);
Thread ot1 = new Thread(new ParameterizedThreadStart(SumData));
ot1.Start(val);
}
}
private static void ReadData(object state)
{
System.Windows.Forms.Application.Run();
}
void setTextboxText(int result)
{
if (this.InvokeRequired)
{
this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result });
}
else
{
SetTextboxTextSafe(result);
}
}
void SetTextboxTextSafe(int result)
{
label1.Text = result.ToString();
}
private static void SumData(object state)
{
int result;
//int[] icount = (int[])state;
int icount = (int)state;
for (int i = icount; i > 0; i--)
{
result += i;
System.Threading.Thread.Sleep(1000);
}
setTextboxText(result);
}
delegate void IntDelegate(int result);
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Why is this error occurring?
An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)
It looks like you are calling a non static member (a property or method, specifically setTextboxText
) from a static method (specifically SumData
). You will need to either:
Make the called member static also:
static void setTextboxText(int result) { // Write static logic for setTextboxText. // This may require a static singleton instance of Form1. }
Create an instance of
Form1
within the calling method:private static void SumData(object state) { int result = 0; //int[] icount = (int[])state; int icount = (int)state; for (int i = icount; i > 0; i--) { result += i; System.Threading.Thread.Sleep(1000); } Form1 frm1 = new Form1(); frm1.setTextboxText(result); }
Passing in an instance of
Form1
would be an option also.Make the calling method a non-static instance method (of
Form1
):private void SumData(object state) { int result = 0; //int[] icount = (int[])state; int icount = (int)state; for (int i = icount; i > 0; i--) { result += i; System.Threading.Thread.Sleep(1000); } setTextboxText(result); }
More info about this error can be found on MSDN.
这篇关于CS0120:非静态字段,方法或属性'foo'需要对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!