简体   繁体   中英

Teach us how to use Delegate and to raise an event in C#

I have 2 classes, Printer and MessageOrigin. Printer instantiates MessageOrigin. In the MessageOrigin class, I can get the messages that I need but I have to give it back to the Printer class so it can be written on the console. I think I have to use the Delegate and Event Raiser to do this. There are many tutorials and explanation out there but it is really hard to understand it let alone implement it. Probably by solving the problem I have here will make it easier to understand how to use this. Please see my sample code below and teach us how to use the delegate and raising of event in C#.

Class Printer
{
    MessageOrigin mo = new MessageOrigin();

    printMessage(string message) {
        console.WriteLine(message) // message here should come from the MessageOrigin class
    }
}

class MessageOrigin
{
    public void GetMessage() {
        var msgs = Get_All_Message();
        SendMessageToPrintClass(msgs);
    }

    public void SendMessageToPrintClass(string message) {
        // how do I send the "message" parameter back to the Printer class
    }
}

Thank you for your patience.

You can use events . The class MessageOrigin will be the publisher, and the class Printer will be a subscriber. The advantage is that later you could add more subscribers, without changing the class MessageOrigin .

class Printer
{
    MessageOrigin mo = new MessageOrigin();

    public Printer()
    {
        mo.NewMessage += PrintMessage; // Subscribe to the event
    }

    void PrintMessage(string message)
    {
        Console.WriteLine(message);
    }
}

class MessageOrigin
{
    public event Action<string> NewMessage; // Declare the event

    public void GetMessage()
    {
        string msgs = Get_All_Message();
        NewMessage?.Invoke(msgs); // Raise the event
    }
}

If I understood correctly, I think you don't need delegates for that (although) you could use it. If you want to have access to the Printer class/object.

My suggestion is (if your architecture allows it) is to pass reference of the printer object to the MessageOrigin class.

You could achieve that this way:

public class Printer
{
    private MessageOrigin mo;

    public Printer() 
    {
        mo = new MessageOrigin(this);
    }


    public void printMessage(string message) 
    {
        Console.WriteLine(message); // message here should come from the MessageOrigin class
    }
}

public class MessageOrigin
{
    private Printer _parentPrinter;
    public MessageOrigin(Printer print) 
    {
        _parentPrinter = print;
    }
    public string Get_All_Message() 
    {
        //implementation
        return string.Empty;
    }
    public void GetMessage() {
        var msgs = Get_All_Message();
        SendMessageToPrintClass(msgs);
    }

    public void SendMessageToPrintClass(string message) {
        // how do I send the "message" parameter back to the Printer class
       _parentPrinter.Message = message //note that you need to implement your message method/property.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM