DEV Community

Kiron Roy
Kiron Roy

Posted on • Updated on

C# Events

Events are used in UI projects, like Windows Forms, WPF (Windows Presentation Foundation).

Events:

  • Alert users that the state of the application has changed
  • An application can listen to an event and act upon what the event wants to do
  • Developers do not control events, because events fire when state changes.
  • There are three parts to and event:
    • The declaration of the event handler
    • The listener to the event
    • Invocation of the event (firing of the event)
ClownSchoolModel.cs
using System;
using System.Collections.Generic;

namespace CoreyEventsHW
{
    public class ClownSchoolModel
    {
        // declaration of event handler 
        public event EventHandler<string> EnrollmentFullClown;

        private List<string> enrolledClowns = new List<string>();
        private List<string> waitList = new List<string>();
        public string ClownClasstitle { get; private set; }
        public int TooManyClowns { get; private set; }

        public ClownSchoolModel(string title, int tooManyClowns)
        {
            ClownClasstitle = title;
            TooManyClowns = tooManyClowns;
        }

        public string SignUpClown(string clownName)
        {
            string output = "";

            if (enrolledClowns.Count < TooManyClowns)
            {
                enrolledClowns.Add(clownName);
                output = $"{clownName} was enrolled in {ClownClasstitle}";

                if (enrolledClowns.Count == TooManyClowns)
                {
                    // invocation of the event (firing of the event)
                    EnrollmentFullClown?.Invoke(this, $"{ClownClasstitle} enrollment is not full. ");
                }
            }

            else
            {
                waitList.Add(clownName);
                output = $"{clownName} was added to the wait list in {ClownClasstitle}";
            }

            return output;
        }
    }
}

Enter fullscreen mode Exit fullscreen mode
ConsoleHelpers.cs

This method is an alternative to Console.WriteLine()

using System;

namespace CoreyEventsHW
{
    public static class ConsoleHelpers
    {
        public static void PrintToConsole(this string message)
        {
            Console.WriteLine(message);
        }
    }
}

Enter fullscreen mode Exit fullscreen mode
Program.cs

This is the main entry to the application

using System;

namespace CoreyEventsHW
{
    class Program
    {
        static void Main(string[] args)
        {
            ClownSchoolModel pieThrowing = new ClownSchoolModel("\"Art of Pie Throwing\"", 3);
            ClownSchoolModel whoopeeCushion = new ClownSchoolModel("\"Whoopee Cushion 101\"", 3);

            // Event listener  wired up
            pieThrowing.EnrollmentFullClown += CollegeClass_EnrollmentFull;

            pieThrowing.SignUpClown("Mita").PrintToConsole();
            pieThrowing.SignUpClown("Lita").PrintToConsole();
            pieThrowing.SignUpClown("Rita").PrintToConsole();
            pieThrowing.SignUpClown("Sita").PrintToConsole();
            pieThrowing.SignUpClown("Gita").PrintToConsole();

            // Event listener  wired up
            whoopeeCushion.EnrollmentFullClown += CollegeClass_EnrollmentFull;

            whoopeeCushion.SignUpClown("Lita").PrintToConsole();
            whoopeeCushion.SignUpClown("Mita").PrintToConsole();
            whoopeeCushion.SignUpClown("Rita").PrintToConsole();
            whoopeeCushion.SignUpClown("Sita").PrintToConsole();
            whoopeeCushion.SignUpClown("Gita").PrintToConsole();

            Console.ReadLine();
        }

        // 1 type of event in 2 different class instances 
        // that are being handled by the same method 

        // Event listener 
        private static void CollegeClass_EnrollmentFull(object sender, string e)
        {
        // right type sent to college model            
            ClownSchoolModel model = (ClownSchoolModel)sender; 
            Console.WriteLine();
            Console.WriteLine($"{model.ClownClasstitle}: Full");
            Console.WriteLine();

        }
    }
}
Enter fullscreen mode Exit fullscreen mode
Output
Mita was enrolled in "Art of Pie Throwing"
Lita was enrolled in "Art of Pie Throwing"

"Art of Pie Throwing": Full

Rita was enrolled in "Art of Pie Throwing"
Sita was added to the wait list in "Art of Pie Throwing"
Gita was added to the wait list in "Art of Pie Throwing"
Lita was enrolled in "Whoopee Cushion 101"
Mita was enrolled in "Whoopee Cushion 101"

"Whoopee Cushion 101": Full

Rita was enrolled in "Whoopee Cushion 101"
Sita was added to the wait list in "Whoopee Cushion 101"
Gita was added to the wait list in "Whoopee Cushion 101"
Enter fullscreen mode Exit fullscreen mode
Repl.it

Discussion (0)