473,389 Members | 1,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 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 1243
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wong chian | last post by:
Hi, I have this easy piece of code: public class Box1:System.web.ui.webcontrols.webcontrol { protected Button mybutton; protected override void OnInit() { InitializeComponent();
2
by: jm | last post by:
I got the code below from the .NET SDK. I am still new to C# and I don't understand how the code works. And by that I mean I don't understand how the program knows to execute it. I can find no...
10
by: Brian | last post by:
Hello all, I have an form to Enter a Name with letters from A to Z in buttons. When you click a button, it should append the text associated with that button to a text box. Instead of...
6
by: Christian Blackburn | last post by:
Hi Gang, I can't seem to figure out how to create an object array in VB.NET 2003. In VB6 you would just select an object copy and paste it and that was that. I don't know why in the heck they...
8
by: Patrick | last post by:
Hello - I'm working on a project to have 6 labels, each with a value of 0 - 9. Each label has a 2 buttons on the side, one labeled up, one labeled down. I want the up button to increment til it...
7
by: Shadow Lynx | last post by:
I realize that his question has been asked, in many other forms, many times in this group. Even so, my tired eyes have not yet found a sufficient answer, so I've decided to "reask" it even though...
3
by: Jonathon | last post by:
In order to make a function handle an even such as a click, do you just have to name the function _EVENT where EVENT is the event that you are trying to handle with the function? I'm creating a...
8
by: John | last post by:
Is there any special code I have to write to log event to Security Event Log? The following code give me "Very Easy Question, How to write log to SECURTY Event Log? Please help" Error // Create...
4
by: Rick Stevens | last post by:
I am not an access expert, could anyone tell me if the following would be easy to do?? I receive emails from a specific email address, that advise me if a specific piece of equipment my company...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.