-
March 25th, 2012, 11:49 AM
#1
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
-
March 26th, 2012, 11:08 AM
#2
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.
-
March 26th, 2012, 11:17 AM
#3
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.
-
March 26th, 2012, 04:25 PM
#4
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
-
March 28th, 2012, 06:46 AM
#5
Re: Delegates and Event
Thanks everyone. This forum has been very helpful to me. Awesome beautiful people here.
-
March 28th, 2012, 07:01 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|