一个对象引用才能访问非静态成员 [英] An object reference is required to access non-static member

查看:99
本文介绍了一个对象引用才能访问非静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计时器,我希望把计时器回调
成不同的功能,但是,我得到这个错误。

 的对象引用才能访问非静态成员..

如果我宣布这些回调的委托和事件
成员变量为静态的,它工作正常。我应该把它呀?

 类MainClass
{
    私人定时器_timer = NULL;
    私人时间跨度_millisecs;    公共静态无效的主要(字串[] args)
    {
        Application.Init();
        主窗口赢=新的主窗口();        标签LBL =新的Label();
        lbl.Text =00:00;        表TBL =新表(2,2,TRUE);
        tbl.Name =TBL;        按钮BTN =新按钮(开始);
        tbl.Attach(LBL,0,2,0,1);
        tbl.Attach(BTN,0,1,1,2);
        按钮btn_stop =新按钮(停止);
        tbl.Attach(btn_stop,1,2,1,2);        btn.Clicked + = StartClick;
        btn_stop.Clicked + = StopClick;        win.Add(TBL);
        win.ShowAll();        Application.Run();
    }    私人无效StartClick(obj对象,EventArgs的参数)
    {
        如果(_timer == NULL){
            _timer =新的Timer();
            _timer.Elapsed + =委托(对象发件人,ElapsedEventArgs E){
                _millisecs = _millisecs.Add(新时间跨度(0,0,0,0,50));
                lbl.Text =新日期时间(_millisecs.Ticks)的ToString(SS:FF);
            };
            _timer.Interval = 50;
            _timer.Enabled = TRUE;
        }        _timer.Start();
    }    私人无效StopClick(obj对象,EventArgs的参数)
    {
        _timer.Stop();
    }}


解决方案

这取决于你想要做什么。你可以的或者的把事情静,的的您可以创建一个实例:

  MainClass实例=新MainClass();
btn.Clicked + = instance.StartClick;
btn_stop.Clicked + = instance.StopClick;

我想这是不是你的真正的应用程序,因此很难说,你应该在你的真实code做什么。个人而言,我会偏向创建一个实例 - 静态变量重新present全局状态是一般的一个坏主意(可测试性等方面)。我们不知道是否会影响您的情况还是不虽然。

重要的是,你了解的为什么的你得到错误信息,为什么上述更改修复它。一旦你理解了,你会在一个更好的局面做出最好的决定。

I have a timer and I want to put timer callbacks into separate functions, however, I get this error.

An object reference is required to access non-static member..

If I declare these callbacks as delegate events and member variables as static, it works fine. Should I leave it that way?

class MainClass
{
    private Timer _timer = null;
    private TimeSpan _millisecs;

    public static void Main (string[] args)
    {
        Application.Init();
        MainWindow win = new MainWindow();

        Label lbl = new Label();
        lbl.Text = "00:00";

        Table tbl = new Table(2, 2, true);
        tbl.Name = "tbl";

        Button btn = new Button("Start");
        tbl.Attach(lbl, 0, 2, 0, 1);
        tbl.Attach(btn, 0, 1, 1, 2);
        Button btn_stop = new Button("Stop");
        tbl.Attach(btn_stop, 1, 2, 1, 2);

        btn.Clicked += StartClick;
        btn_stop.Clicked += StopClick;

        win.Add(tbl);
        win.ShowAll();

        Application.Run ();
    }

    private void StartClick(object obj, EventArgs args)
    {
        if (_timer == null) {
            _timer = new Timer();
            _timer.Elapsed += delegate(object sender, ElapsedEventArgs e) {
                _millisecs = _millisecs.Add(new TimeSpan(0, 0, 0, 0, 50));
                lbl.Text = new DateTime(_millisecs.Ticks).ToString("ss:ff");
            };
            _timer.Interval = 50;
            _timer.Enabled = true;                  
        }

        _timer.Start();             
    }

    private void StopClick(object obj, EventArgs args)
    {
        _timer.Stop();          
    }

}

解决方案

It depends what you're trying to do. You can either make things static, or you can create an instance:

MainClass instance = new MainClass();
btn.Clicked += instance.StartClick;
btn_stop.Clicked += instance.StopClick;

I assume this isn't your real application, so it's hard to say what you should do in your real code. Personally I'd lean towards creating an instance - static variables represent global state which is usually a bad idea (in terms of testability etc). We can't tell whether that affects your situation or not though.

What's important is that you understand why you're getting the error message and why the above change fixes it. Once you understand that, you'll be in a better situation to make the best decisions.

这篇关于一个对象引用才能访问非静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆