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 8 1244
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
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
> 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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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();
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |