CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Delegates and Event

  1. #1
    Join Date
    Mar 2012
    Posts
    12

    Delegates and Event

    Hi all

    I am trying to use a code sniplet from a source, but the error CS0079 was prompted, saying that StatusMessageChange should only appear on the left side of += or -+. Can someone kindly help me.
    Code:
    namespace IBIX
    {
    	public class BatchInfo
    	{
    		public delegate void StatusMessageChangeHandle(object tmp, StatusMessageChangeEventArgs e);
    
    
    		public event BatchInfo.StatusMessageChangeHandle StatusMessageChange
    		{
    			add
    			{
    				this.StatusMessageChange = (BatchInfo.StatusMessageChangeHandle)Delegate.Combine(this.StatusMessageChange, value);
    			}
    			remove
    			{
    				this.StatusMessageChange = (BatchInfo.StatusMessageChangeHandle)Delegate.Remove(this.StatusMessageChange, value);
    			}
    		}
    
    }
    }
    Much appreciated.
    Last edited by Cimperiali; March 26th, 2012 at 11:09 AM. Reason: added [code][/code] tags

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Delegates and Event

    sounds like (but I did not tested) you're looking for:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DelegateCombineCSharp
    {
        public class BatchInfo
        {
            public delegate void StatusMessageChangeHandle(object tmp, StatusMessageChangeEventArgs e);
    
            private   BatchInfo.StatusMessageChangeHandle _StatusMessageChange;
            public event BatchInfo.StatusMessageChangeHandle StatusMessageChange
            {
                add
                {
                    this._StatusMessageChange += (BatchInfo.StatusMessageChangeHandle)Delegate.Combine(this._StatusMessageChange, value);
                }
                remove
                {
                    this._StatusMessageChange -= (BatchInfo.StatusMessageChangeHandle)Delegate.Remove(this._StatusMessageChange, value);
                }
            }
    
        }
        
    }
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Delegates and Event

    wait: it should be as follows:
    Code:
     public class BatchInfo
        {
            public delegate void StatusMessageChangeHandle(object tmp, StatusMessageChangeEventArgs e);
    
            private  BatchInfo.StatusMessageChangeHandle _StatusMessageChange;
            public event BatchInfo.StatusMessageChangeHandle StatusMessageChange
            {
                add
                {
                    this._StatusMessageChange = (BatchInfo.StatusMessageChangeHandle)Delegate.Combine(this._StatusMessageChange, value);
                }
                remove
                {
                    this._StatusMessageChange = (BatchInfo.StatusMessageChangeHandle)Delegate.Remove(this._StatusMessageChange, value);
                }
            }
    
        }
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Delegates and Event

    Code:
    namespace WindowsFormsApplication1
    {
       public partial class Form1 : Form
       {
           private BatchInfo info;
           /// <summary>
           /// Constructzor
           /// </summary>
           public Form1()
           {
               InitializeComponent();
               info = new BatchInfo(); // BatchInfo class is crreated here
               // delegate is added here
               info.StatusMessageChange += new BatchInfo.StatusMessageChangeHandle(info_StatusMessageChange);
           }
           /// <summary>
           /// Using the event outside the class
           /// </summary>
           /// <param name="tmp"></param>
           /// <param name="e"></param>
           void info_StatusMessageChange(object tmp, EventArgs e)
           {
              /// Your code is here
           }
     
       }
       public class BatchInfo
       {
           public delegate void StatusMessageChangeHandle(object tmp, EventArgs e);
           private event StatusMessageChangeHandle statusChange;
           public BatchInfo() {
               this.StatusMessageChange += new StatusMessageChangeHandle(BatchInfo_StatusMessageChange);
           }
           /// <summary>
           /// A delegate used within the class itself
           /// </summary>
           /// <param name="tmp"></param>
           /// <param name="e"></param>
           void BatchInfo_StatusMessageChange(object tmp, EventArgs e)
           {
               // Your code is here
           }
     
           /// <summary>
           /// Here is the event explicitly typed
           /// </summary>
           public event StatusMessageChangeHandle StatusMessageChange
           {
               add
               {
                   this.statusChange  += value;
                   // you adfdress the private event or you will get Stackoverflow
                   // because of redirecting to the same name!!!
               }
               remove
               {
                   this.statusChange -= value; 
               }
           }
       }
    }
    Hope this helps a bit- Shows how to use a delegate inside a class and outsid a class
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Mar 2012
    Posts
    12

    Re: Delegates and Event

    Thanks everyone. This forum has been very helpful to me. Awesome beautiful people here.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Delegates and Event

    In this scenario you could simply write:


    public event BatchInfo.StatusMessageChangeHandle StatusMessageChange;

    This will implicitly have the normal "add" and "remove" handlers. I'm just mentioning this in case you thought you had to write the add/remove handlers similar to how a property requires you write get/set methods.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)