Monday, July 29, 2013

MVP with Events and Delegates Simple Example

Events and Delegates Simple Example

With Delegates

using System;
 
namespace ConsoleApplication1
{
    public delegate void MyDelegate(int state);
 
    class Program
    {
        static void Main(string[] args)
        {
            var view = new View();
            new Presenter(view);
            view.ViewDelete();
        }
    }
 
    public class View
    {
        public delegate void VDelete();
 
        public event VDelete Delete;
 
        public void ViewDelete()
        {
            VDelete vd = new VDelete(Delete);
 
            if (Delete != null)
            {
                vd();
            }
        }
    }
 
    public class Presenter
    {
        public Presenter(View view)
        {
            view.Delete += view_Delete;
        }
 
        void view_Delete()
        {
 
        }
    }
}
 



Without Delegates
using System;
 
namespace ConsoleApplication1
{
    public delegate void MyDelegate(int state);
 
    class Program
    {
        static void Main(string[] args)
        {
            var view = new View();
            var presenter = new Presenter(view);
            view.ViewDelete();
        }
    }
 
 
 
 
 
    public class View
    {
        public event EventHandler Delete;
 
        public void ViewDelete()
        {
            Delete(thisEventArgs.Empty);
        }
    }
 
 
 
    public class Presenter
    {
        public Presenter(View view)
        {            
            view.Delete += view_Delete;
        }
 
        void view_Delete(object sender, EventArgs e)
        {
 
        }
    }
}

No comments:

Post a Comment