8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

CS0120:非静态字段、方法或属性“foo”需要对象引用

Squidgybuffalo 1月前

75 0

考虑:命名空间 WindowsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Cl...

考虑:

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();
        }
    }
}

为什么会发生此错误?

非静态字段、方法或属性“WindowsApplication1.Form1.setTextboxText(int)”需要对象引用

帖子版权声明 1、本帖标题:CS0120:非静态字段、方法或属性“foo”需要对象引用
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Squidgybuffalo在本站《visual-studio》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 您的问题的本质和解决方案是这样的:

    using System;
    
    namespace myNameSpace
    {
        class Program
        {
            private void method()
            {
                Console.WriteLine("Hello World!");
            }
    
            static void Main(string[] args)
            {
                method();//<-- Compile Time error because an instantiation of the Program class doesnt exist
                Program p = new Program();
                p.method();//Now it works. (You could also make method() static to get it to work)
            }
        }
    }
    
返回
作者最近主题: