Can someone please tell me why I’m getting the error “object reference is required” for the call to CheckForMessage” in the last line. Thanks.
class Program
{
private void CheckForMessage(object source, ElapsedEventArgs e)
{
Random random = new Random();
Console.WriteLine("Checking for new Messages");
if ((random.Next(9) == 0)) { Console.WriteLine("hello mum"); } else { Console.WriteLine("no message"); }
}
static void Main(string[] args)
{
Timer pollTimer=new Timer(100);
pollTimer.Elapsed+=new ElapsedEventHandler(CheckForMessage);
}
}
You’re trying to call an instance method (
CheckForMessage) without an instance of a class. Just change it to a static method instead: