Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Using Events/Delegates to Subscribe To A Service - Emails
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static int ID; //Main program Function public static void Main(string[] args) { Run(); } /*public static void CreateEmail() { ID += 1; Console.WriteLine("New Email\nEnter Title: "); String title = Console.ReadLine(); Console.WriteLine("Enter Subject: "); String subject = Console.ReadLine(); Console.WriteLine("Enter Recipient: "); String recipient = Console.ReadLine(); Console.WriteLine("Enter Body: "); String body = Console.ReadLine(); NewEmail letter = new NewEmail(ID, title, subject, recipient, body); Run(letter); }*/ public static void Run() { //Email Class Variables var letter = new NewEmail(1, "Here's one secret to success", "The only way!", "Shia Le Boeuf", "Just do it!"); var sendEmail = new NewEmailSend(); var messageSender = new MessageSender(); var textSender = new TextSender(); //Subscribing the Email through MessageSender sendEmail.EmailEvent += messageSender.OnSend; sendEmail.EmailEvent += textSender.OnSend; //Putting Email through Send Function sendEmail.SendingEmail(letter); } } //The New Email Class public class NewEmail { //Email Variables public int ID {get; set;} public String title {get; set;} public String subject {get; set;} public String receiver {get; set;} public String body {get; set;} //New Email Constructor - Program Passes Email Data Here public NewEmail(int ID, String title, String subject, String receiver, String body) { this.ID = ID; this.title = title; this.subject = subject; this.receiver = receiver; this.body = body; } } //The Email Sending Process public class NewEmailSend { public event EventHandler<EmailDelegate> EmailEvent; //When Sending public void SendingEmail(NewEmail ne) { Console.WriteLine("Sending Email..."); OnSend(ne); } //When Sent public void OnSend(NewEmail ne) { if(EmailEvent != null) { EmailEvent(this, new EmailDelegate() {NewEmail = ne}); } } } //The Email Delegate public class EmailDelegate { public NewEmail NewEmail {get; set;} } //The Subscribed Service - Aka Messanger Message public class MessageSender { public void OnSend(object source, EmailDelegate email) { Console.WriteLine("Passing Email ID: " + email.NewEmail.ID + " To Message Service"); SendEmail(email); } public void SendEmail(EmailDelegate email) { } } //The Subscribed Service - Aka Text Message public class TextSender { public void OnSend(object source, EmailDelegate email) { Console.WriteLine("Passing Email ID: " + email.NewEmail.ID + " To Text Service"); SendEmail(email); } public void SendEmail(EmailDelegate email) { } } }
run
|
edit
|
history
|
help
0
Fortnite Generator
azs dasx
a
w
Override method/function in C#
enigma 2
and
ok
C#: Strings / Char Test
GetDataFromSQLGroups