访问非静态成员需要对象引用

发布于 11-28 17:57 字数 1500 浏览 0 评论 0原文

我有一个计时器,我想放置计时器回调 到单独的函数中,但是,我收到此错误。

访问非静态字段、方法或属性需要对象引用''...

如果我将这些回调声明为委托事件并且 成员变量作为静态的,它工作得很好。我应该就这样留下吗?

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

}

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 field, method, or property ''...

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深巷少女2024-12-05 17:57:18

这取决于你想做什么。您可以或者使事物静态,或者您可以创建一个实例:

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

我假设这不是您的真实应用程序,所以很难说你应该在实际代码中做什么。就我个人而言,我倾向于创建一个实例 - 静态变量代表全局状态,这通常是一个坏主意(就可测试性等而言)。但我们无法判断这是否会影响您的情况。

重要的是您了解为什么收到错误消息以及为什么上述更改可以解决该问题。一旦你明白了这一点,你就能更好地做出最好的决定。

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.

や三分注定2024-12-05 17:57:18

这是因为在这种情况下您正在从静态访问非静态方法。

要么创建类的对象,然后

ObjectOfClass obj = new ObjectOfClass();
obj.MethodName();

在您的情况下

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

调用该方法,要么将被调用的方法创建为静态(您已经按照您所说的那样尝试过)。

It's because you are accessing non-static methods from the static in this case.

Either create object of the class and than call the method

ObjectOfClass obj = new ObjectOfClass();
obj.MethodName();

so in your case

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

Or create the called methods as static (which you already tried as you said).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文