471,317 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

easy Event Q


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.

Thanks in advance for all help
Feb 2 '06 #1
8 1208
TheMadHatter <Th**********@discussions.microsoft.com> wrote:
Easy Q:
Can an event be directly raised from outside of the class?


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 2 '06 #2
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.

"Jon Skeet [C# MVP]" wrote:
TheMadHatter <Th**********@discussions.microsoft.com> wrote:
Easy Q:
Can an event be directly raised from outside of the class?


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 2 '06 #3
> 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.


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.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Feb 2 '06 #4
TheMadHatter wrote:
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.


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

Feb 2 '06 #5
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????

I dont quite understand...
Could you give a one line example?
eg
SomeClass.ClassDeligate = new SomeClass.ClassDeligate(?????????????????????

"Jon Skeet [C# MVP]" wrote:
TheMadHatter wrote:
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.


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

Feb 2 '06 #6
TheMadHatter wrote:
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????
A delegate can, but an instance can't.
I dont quite understand...
Could you give a one line example?
eg
SomeClass.ClassDeligate = new SomeClass.ClassDeligate(?????????????????????


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.WriteLine ("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. (Experimentation is
good, but only with a good book to refer to in order to check your
results etc!)

Jon

Feb 2 '06 #7
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:
TheMadHatter wrote:
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????


A delegate can, but an instance can't.
I dont quite understand...
Could you give a one line example?
eg
SomeClass.ClassDeligate = new SomeClass.ClassDeligate(?????????????????????


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.WriteLine ("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. (Experimentation is
good, but only with a good book to refer to in order to check your
results etc!)

Jon

Feb 2 '06 #8
TheMadHatter wrote:
Oh..... THAT public. ic.
No that is not what I want at all.
Thanks for every thing, but the solution I have seams better.


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.

Jon

Feb 2 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Wong chian | last post: by
2 posts views Thread by jm | last post: by
10 posts views Thread by Brian | last post: by
7 posts views Thread by Shadow Lynx | last post: by
3 posts views Thread by Jonathon | last post: by
4 posts views Thread by Rick Stevens | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.