Resolved can't print my messages?

xQualify

New member
Joined
Nov 13, 2020
Messages
3
Programming Experience
1-3
I'm making task about listeners and events, can't print my messages.. here's my simple code(that doesn't print anything)

C#:
using System;
class Program
{
    public delegate void Show(string _msg);
    public static event Show OnShow;

    public delegate void Three(int _amount);
    public static event Three OnThree;

    public delegate void Messages(string _word, string _secondWord);
    public static event Messages OnMessages;

    static void Main()
    {
        Listener listener = new Listener();
       
        //Raise events
        if (OnShow != null)
            OnShow("Display ");

        if (OnThree != null)
            OnThree(3);

        if (OnMessages != null)
            OnMessages(" different", " messages");
    }
}

class Listener
{
    public void ListenToProgramEvents()
    {
        //Add your listeners HERE
        Program.OnShow += HandleShow;
        Program.OnThree += HandleThree;
        Program.OnMessages += HandleMessages;
    }
        //Appropriate methods HERE (that are called when events are received from Program)
    public void HandleShow(string _msg)
    {
        Console.WriteLine(_msg);
       
    }
    private void HandleThree(int _amount)
    {
        Console.WriteLine(_amount);
    }
    private void HandleMessages(string _word, string _secondWord)
    {
        Console.WriteLine(_word + _secondWord);
    }
    //Expected result: print "Display 3 different messages" to console

}
 
Last edited by a moderator:
Solution
Look in your main();.

How do you expect the events to fire before you have created them?

So it's only right that they are null, which is why your code doesn't execute.

Fix it by calling the method that creates them first :
C#:
        Listener listener = new Listener();
        listener.ListenToProgramEvents();

Marking as solved.
Are you running from the console or in Visual Studio? You should get output if running from the console.

If from Visual Studio, for versions prior to VS2019 (?), console programs would quickly display in a console window and then close. You need to add a pause or wait for additional input. Alternatively you can Run Without Debugger (Ctrl-F5).

With VS2019, the new default is to keep the console window open for you.
 
Have you debugged the code? At line 18 is OnShow null or not?
 
If line 34 executes, then it shouldn't be null on line 18.
 
Then you need to find out why it is null, as pointed out it should be assigned at line 34 in the method ListenToProgramEvents, is this method not called in your code?
 
Look in your main();.

How do you expect the events to fire before you have created them?

So it's only right that they are null, which is why your code doesn't execute.

Fix it by calling the method that creates them first :
C#:
        Listener listener = new Listener();
        listener.ListenToProgramEvents();

Marking as solved.
 
Solution
Before calling ListenToProgramEvents : Screenshot
After calling ListenToProgramEvents : Screenshot

Glad you figured it out @Sheepings (y)
An easy fix, and easy to miss when you're tired. I assume our OP may be tired. Or maybe they assumed when you instantiate an object, that it initialises all methods within by calling them itself. :eek:

Even in my most exhausted state I surprise myself sometimes. ;)
 
Back
Top Bottom