Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 02:35 PM
=?Utf-8?B?anAybXNmdA==?=
Guest
 
Posts: n/a
Default How to Create Custom Events?

I know how to use events, but how can I create a custom event?

Take the pointless class below:

class MyInt
{
int n;
public MyInt(int value)
{
n = value;
}
}

How would I create an event for it so that I can call a catching method
whenever the value is changed?
  #2  
Old August 28th, 2008, 02:45 PM
cfps.Christian
Guest
 
Posts: n/a
Default Re: How to Create Custom Events?

I know how I would do it:

public class IntEventArgs : EventArgs
{
private int _value;

public IntEventArgs(int Value)
{
_value = Value;
}

public int Value
{
get { return _value; }
}
}

public class MyInt
{
private int n;
public delegate void ValueChangedDelegate(object sender,
IntEventArgs e);
public event ValueChangedDelegate;

public MyInt(int value)
{
n = value;
}

public int Value
{
get { return n; }
set
{
n = value;
if (ValueChanged != null)
ValueChanged(this, new IntEventArgs(n);
}
}
}
}

  #3  
Old August 28th, 2008, 03:55 PM
Peter Duniho
Guest
 
Posts: n/a
Default Re: How to Create Custom Events?

On Thu, 28 Aug 2008 06:29:01 -0700, jp2msft
<jp2msft@discussions.microsoft.comwrote:
Quote:
I know how to use events, but how can I create a custom event?
>
Take the pointless class below:
>
class MyInt
{
int n;
public MyInt(int value)
{
n = value;
}
}
>
How would I create an event for it so that I can call a catching method
whenever the value is changed?
The other reply would work (almost...it's got at least one error, easily
found and fixed). But the canonical pattern for this sort of thing is to
just use a regular EventHandler:

class MyInt
{
int n;

public event EventHandler NChanged;

public MyInt(int n)
{
this.n = n;
}

public int N
{
get { return n; }
set
{
n = value;
RaiseNChanged();
}
}

private void RaiseNChanged()
{
EventHandler handler = NChanged;

if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}

(Of course, you can name the property anything else...it doesn't have to
be "N")

So now you have two options. :)

Pete
  #4  
Old August 28th, 2008, 04:35 PM
=?Utf-8?B?anAybXNmdA==?=
Guest
 
Posts: n/a
Default Re: How to Create Custom Events?

Great!

Now, on to how to make it impliment easily:

I see your code (sticking with the N property you used and I neglected to
include) would result in something like this:

MyInt Count = new MyInt(0);
// and alter to use it....
Count.N = Count.N + intNewValue;

Could the class be modified so that the workings of it are more transparent
(i.e. more like an 'int' or whatever value I am trying to mimic)?

Example:
MyInt Count = 0;
Count += intNewValue;

This is some cool theory stuff that typically is difficult to find.

"Peter Duniho" wrote:
Quote:
On Thu, 28 Aug 2008 06:29:01 -0700, jp2msft
<jp2msft@discussions.microsoft.comwrote:
>
Quote:
I know how to use events, but how can I create a custom event?

Take the pointless class below:

class MyInt
{
int n;
public MyInt(int value)
{
n = value;
}
}

How would I create an event for it so that I can call a catching method
whenever the value is changed?
>
The other reply would work (almost...it's got at least one error, easily
found and fixed). But the canonical pattern for this sort of thing is to
just use a regular EventHandler:
>
class MyInt
{
int n;
>
public event EventHandler NChanged;
>
public MyInt(int n)
{
this.n = n;
}
>
public int N
{
get { return n; }
set
{
n = value;
RaiseNChanged();
}
}
>
private void RaiseNChanged()
{
EventHandler handler = NChanged;
>
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
>
(Of course, you can name the property anything else...it doesn't have to
be "N")
>
So now you have two options. :)
>
Pete
>
  #5  
Old August 28th, 2008, 05:05 PM
Peter Duniho
Guest
 
Posts: n/a
Default Re: How to Create Custom Events?

On Thu, 28 Aug 2008 08:28:02 -0700, jp2msft
<jp2msft@discussions.microsoft.comwrote:
Quote:
[...]
Could the class be modified so that the workings of it are more
transparent
(i.e. more like an 'int' or whatever value I am trying to mimic)?
>
Example:
MyInt Count = 0;
Count += intNewValue;
Sure. You can overload the + operator for the class, to allow instances
of the class to be added to each other, as well as to provide for adding
an int to your class.

That said, I'll warn that things may start to get confusing if you do
this. Operator overloads can work very nicely for value types, but for a
mutable class, having a binary operator (for example) modify one of the
operands can result in some very non-intuitive code.

In particular, you can't overload +=. You have to overload +, and then
the compiler uses that overload when implementing +=. That means that if
this:

MyInt Count = new MyInt(0); // no overload of assignment operator
Count += intNewValue;

did what you expected, then this:

MyInt Count = new MyInt(0);
MyInt Other = Count + intNewValue;

might not do what you expect ("Other" would wind up referencing the same
instance as "Count", and that single instance of MyInt would have been
modified).

That's because the overload would have to look like this:

public static MyInt operator +(MyInt op1, int op2)
{
op1.N += op2;
return op1;
}

Now, all that said, you could write this:

MyInt Count = new MyInt(0); // no overload of assignment operator
Count.N += intNewValue;

without doing any operator overloading. That's almost as concise as your
desired syntax, but preserves the normal semantics for a mutable reference
type.

Pete
  #6  
Old August 28th, 2008, 06:48 PM
=?Utf-8?B?anAybXNmdA==?=
Guest
 
Posts: n/a
Default Re: How to Create Custom Events?

Beautiful! I'm writing all this down in an email that I'm forwarding to
myself for safe keeping.

I can't wait to start messing around with this new trick.

Thanks, Mr. Duniho!

"Peter Duniho" wrote:
Quote:
On Thu, 28 Aug 2008 08:28:02 -0700, jp2msft
<jp2msft@discussions.microsoft.comwrote:
>
Quote:
[...]
Could the class be modified so that the workings of it are more
transparent
(i.e. more like an 'int' or whatever value I am trying to mimic)?

Example:
MyInt Count = 0;
Count += intNewValue;
>
Sure. You can overload the + operator for the class, to allow instances
of the class to be added to each other, as well as to provide for adding
an int to your class.
>
That said, I'll warn that things may start to get confusing if you do
this. Operator overloads can work very nicely for value types, but for a
mutable class, having a binary operator (for example) modify one of the
operands can result in some very non-intuitive code.
>
In particular, you can't overload +=. You have to overload +, and then
the compiler uses that overload when implementing +=. That means that if
this:
>
MyInt Count = new MyInt(0); // no overload of assignment operator
Count += intNewValue;
>
did what you expected, then this:
>
MyInt Count = new MyInt(0);
MyInt Other = Count + intNewValue;
>
might not do what you expect ("Other" would wind up referencing the same
instance as "Count", and that single instance of MyInt would have been
modified).
>
That's because the overload would have to look like this:
>
public static MyInt operator +(MyInt op1, int op2)
{
op1.N += op2;
return op1;
}
>
Now, all that said, you could write this:
>
MyInt Count = new MyInt(0); // no overload of assignment operator
Count.N += intNewValue;
>
without doing any operator overloading. That's almost as concise as your
desired syntax, but preserves the normal semantics for a mutable reference
type.
>
Pete
>
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles