Jump to content

Can anyone show me an example of a delegate and an event?

Dobbsjr
Go to solution Solved by Pinguinsan,
namespace DelegateTest
{
    public class Car
    {
        //Note, delegate and event must have the exact same name
        public delegate void PositionChanged(int newPosition);
        public event PositionChanged positionChangedEvent;

        private int m_position;

        public Car()
        {
            this.m_position = 0; //car is at position 0
        }

        public void driveToPosition(int position)
        {
            if (this.m_position != position) //check if position changed
            {
                this.m_position = position;
                //Always check event for null before triggering the event 
                if (this.positionChangedEvent != null)
                {
                    this.positionChangedEvent.Invoke(this.m_position);
                }
            }

            
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            car.positionChangedEvent += Program.onCarPositionChanged; //Hook/subscribe to the event
            car.driveToPosition(15);
            car.driveToPosition(200);
            car.driveToPosition(0);
            car.driveToPosition(0); //Will not fire event, because no change in position
            car.driveToPosition(10);
            Console.ReadKey(); //Pause console to see output
            //...etc
        }

        //Note that the receiving function must have the same type signature as the delegate/event
        private static void onCarPositionChanged(int newPosition)
        {
            Console.WriteLine("The car moved to position {0}", newPosition);
        }
    }
}

 

I understand what both are conceptually(a delegate is a way to encapsulate a function inside a variable and event is a way of notifying other objects), but I've been having trouble implementing them into a real program. Just a quick and simple example program would help a lot. Thanks in advance. 

Did my post help you? Then make sure to rate it!

Check out my post on symbolic links! || PSU ranking and tiers || Pokemon Thread

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Dobbsjr said:

I understand what both are conceptually(a delegate is a way to encapsulate a function inside a variable and event is a way of notifying other objects), but I've been having trouble implementing them into a real program. Just a quick and simple example program would help a lot. Thanks in advance. 

I don't have the code on me but I recall many years ago in my last job when I worked with C# apps as engineering tools, we'd use a serial port to communicate with our devices. The thing with .NET is that it does not like it if the main application tries to access the serial port stuff (cross thread access violation). You'd use a delegate to call the serial data parser to safely pass the data from the serial port thread to the application thread.

 

I think I did the same thing with events too, but it was more of an exercise than putting it to practical use.

Link to comment
Share on other sites

Link to post
Share on other sites

namespace DelegateTest
{
    public class Car
    {
        //Note, delegate and event must have the exact same name
        public delegate void PositionChanged(int newPosition);
        public event PositionChanged positionChangedEvent;

        private int m_position;

        public Car()
        {
            this.m_position = 0; //car is at position 0
        }

        public void driveToPosition(int position)
        {
            if (this.m_position != position) //check if position changed
            {
                this.m_position = position;
                //Always check event for null before triggering the event 
                if (this.positionChangedEvent != null)
                {
                    this.positionChangedEvent.Invoke(this.m_position);
                }
            }

            
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            car.positionChangedEvent += Program.onCarPositionChanged; //Hook/subscribe to the event
            car.driveToPosition(15);
            car.driveToPosition(200);
            car.driveToPosition(0);
            car.driveToPosition(0); //Will not fire event, because no change in position
            car.driveToPosition(10);
            Console.ReadKey(); //Pause console to see output
            //...etc
        }

        //Note that the receiving function must have the same type signature as the delegate/event
        private static void onCarPositionChanged(int newPosition)
        {
            Console.WriteLine("The car moved to position {0}", newPosition);
        }
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×