C#简单的事件处理程序设置报警 [英] C# Simple Event Handler for Setting Alarm
本文介绍了C#简单的事件处理程序设置报警的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么以下行alarm.AlarmEvent + =新AlarmEventHandler(alarm_Sound);给我的对象引用是必需的非静态字段,方法或属性AlarmClock.Alarm.alarm_Sound(对象,System.EventArgs)
Why does the following line "alarm.AlarmEvent += new AlarmEventHandler(alarm_Sound);" gives me "An object reference is required for the non-static field, method, or property 'AlarmClock.Alarm.alarm_Sound(object, System.EventArgs)'"
public static void Main(string[] args)
{
Alarm alarm = new Alarm(new DateTime(2010, 4, 7, 23, 2, 0));
alarm.Set();
alarm.AlarmEvent += new AlarmEventHandler(alarm_Sound);
}
完整的源代码在这里:
的 Program.cs的
的 AlarmEventArgs
推荐答案
您要添加的事件处理程序后,调用设置
方法。结果
因此,当设置
方法引发事件,这不是'T有一个处理程序呢。
You're adding the event handler after calling the Set
method.
Therefore, when the Set
method raises the event, it doesn't have a handler yet.
这篇关于C#简单的事件处理程序设置报警的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文