Quote:
quote:Originally posted by elwappo
I presume when you say you can reuse the same delegate for multiple events, it could be declared in a helper class and called when needed?
|
Yes, you certainly can put your delegates in a reusable location. When I define delegates for something specific I usually keep them with the related functionality. But general use delegates would go into a generic delegates file in the project somewhere.
One important semantic here though: At least within the context of this conversion, you don't "call" a delegate. In this case we are defining delegates for purpose of defining event signatures. So you define an even on a class that matches that delegate signature. The class defining the event will eventually raise it. What you are really going is calling the "Invoke" method of a class (your delegate) that is derived from System.MulticastDelegate.
Quote:
quote:I presume then, you can reuse my "Finished" Event, as you mention there is no difference in my two declarations.
|
The only reuse you can make of an event defined on a class is to raise that event more than once. The event is just a member of the class, no different than a property or method. You reuse the types that define them and, by extension, those members. So you can create many instances of a class that has methods and the event is part of it so it is available to use.
Quote:
quote:From reading articles online/in books, event handling/delegates are a simple process that is explained very badly...
|
The problem I have found is that delegates are a tricky beast because they are used in different ways. As I've mentioned, a delegate is really just a class based on a specific type. However, because of the nature of the event driven model of .NET dealing with events is such a core aspect so there are language constructs that obfuscate the use of the delegate classes.
When we are dealing with class events we create "events" instead of creating explicit instances of a delegate class. Then instead of explicitly calling 'myEvent.Invoke(...)' we can call RaiseEvent myEvent() [
VB] or just myEvent() [C#]. The need to think about events as class instances is abstracted away by the languages.
You can also use delegate instances as method callbacks. In this case they are treated more like data. In this case the data is a pointer to a method. You call that method like you call any other method, but instead of the method name you use the variable name of the delegate instance.
-Peter
compiledthoughts.com