Easy Q:
Can an event be directly raised from outside of the class?
I can figure out how to use a method to indirectly raise an
event, but I havent found a way to do it directly.
TheMadHatter <TheMadHatter@d iscussions.micr osoft.com> wrote:[color=blue]
> Easy Q:
> Can an event be directly raised from outside of the class?[/color]
No. The point of an event is that it allows clients to
subscribe/unsubscribe. If you need to be able to fire it as well, just
use a delegate instead (if you control the code).
If I use a delegate, I still cant get the desired effect.
I guess the only thing I can do, is to use a method, to
raise the event. It is clean, but I am always looking for
a better way.
"Jon Skeet [C# MVP]" wrote:
[color=blue]
> TheMadHatter <TheMadHatter@d iscussions.micr osoft.com> wrote:[color=green]
> > Easy Q:
> > Can an event be directly raised from outside of the class?[/color]
>
> No. The point of an event is that it allows clients to
> subscribe/unsubscribe. If you need to be able to fire it as well, just
> use a delegate instead (if you control the code).
>
> --
> Jon Skeet - <skeet@pobox.co m>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>[/color]
> Hum..... Thanks for the responce.[color=blue]
>
> If I use a delegate, I still cant get the desired effect. I guess the
> only thing I can do, is to use a method, to raise the event. It is
> clean, but I am always looking for a better way.[/color]
This is why you see those OnEventName methods in a lot of classes, to explicitly
allow code outside of the class to fire the event.
TheMadHatter wrote:[color=blue]
> Hum..... Thanks for the responce.
>
> If I use a delegate, I still cant get the desired effect.
> I guess the only thing I can do, is to use a method, to
> raise the event. It is clean, but I am always looking for
> a better way.[/color]
What do you mean, you can't get the desired effect? If you make the
delegate field public, then anyone can subscribe, unsubscribe, or fire
the event.
I wouldn't advise that, personally - I'd still use an event, but expose
a method to raise the event (as you described). That's about as good as
it gets :)
Okay, so the deligate can't be called via an instance of the
object, but it can be called kinda like a static shared function????
I dont quite understand...
Could you give a one line example?
eg
SomeClass.Class Deligate = new SomeClass.Class Deligate(?????? ???????????????
"Jon Skeet [C# MVP]" wrote:
[color=blue]
> TheMadHatter wrote:[color=green]
> > Hum..... Thanks for the responce.
> >
> > If I use a delegate, I still cant get the desired effect.
> > I guess the only thing I can do, is to use a method, to
> > raise the event. It is clean, but I am always looking for
> > a better way.[/color]
>
> What do you mean, you can't get the desired effect? If you make the
> delegate field public, then anyone can subscribe, unsubscribe, or fire
> the event.
>
> I wouldn't advise that, personally - I'd still use an event, but expose
> a method to raise the event (as you described). That's about as good as
> it gets :)
>
> Jon
>
>[/color]
TheMadHatter wrote:[color=blue]
> Slowly but surely I am figuring it all out....
>
> Okay, so the deligate can't be called via an instance of the
> object, but it can be called kinda like a static shared function????[/color]
A delegate can, but an instance can't.
[color=blue]
> I dont quite understand...
> Could you give a one line example?
> eg
> SomeClass.Class Deligate = new SomeClass.Class Deligate(?????? ???????????????[/color]
using System;
public delegate void SimpleDelegate( );
public class Foo
{
public SimpleDelegate testDelegate;
}
public class Test
{
static void Main()
{
Foo f = new Foo();
f.testDelegate += new SimpleDelegate( OtherMethod);
f.testDelegate( );
}
static void OtherMethod()
{
Console.WriteLi ne ("Hi");
}
}
However, having a public field like that isn't a good idea.
Unfortunately I don't have time to go into events and delegates at
length just now - but I strongly suggest you find a good book or
tutorial to read on the topic. It can be very confusing if you try to
learn it just by experimentation , unfortunately. (Experimentatio n is
good, but only with a good book to refer to in order to check your
results etc!)
Oh..... THAT public. ic.
No that is not what I want at all.
Thanks for every thing, but the solution I have seams better.
"Jon Skeet [C# MVP]" wrote:
[color=blue]
> TheMadHatter wrote:[color=green]
> > Slowly but surely I am figuring it all out....
> >
> > Okay, so the deligate can't be called via an instance of the
> > object, but it can be called kinda like a static shared function????[/color]
>
> A delegate can, but an instance can't.
>[color=green]
> > I dont quite understand...
> > Could you give a one line example?
> > eg
> > SomeClass.Class Deligate = new SomeClass.Class Deligate(?????? ???????????????[/color]
>
> using System;
>
> public delegate void SimpleDelegate( );
>
> public class Foo
> {
> public SimpleDelegate testDelegate;
> }
>
> public class Test
> {
> static void Main()
> {
> Foo f = new Foo();
> f.testDelegate += new SimpleDelegate( OtherMethod);
> f.testDelegate( );
> }
>
> static void OtherMethod()
> {
> Console.WriteLi ne ("Hi");
> }
> }
>
> However, having a public field like that isn't a good idea.
>
> Unfortunately I don't have time to go into events and delegates at
> length just now - but I strongly suggest you find a good book or
> tutorial to read on the topic. It can be very confusing if you try to
> learn it just by experimentation , unfortunately. (Experimentatio n is
> good, but only with a good book to refer to in order to check your
> results etc!)
>
> Jon
>
>[/color]
TheMadHatter wrote:[color=blue]
> Oh..... THAT public. ic.
> No that is not what I want at all.
> Thanks for every thing, but the solution I have seams better.[/color]
Absolutely. (Of course, you could expose the delegate through a
property, or something like that - the results would be the same as
with the public field; I only used a field to keep it simple here.)
The "event + raising method" is a pretty elegant solution.
Comment