R
raylopez99
Regarding delegates used as events in the .NET framework--I've
discovered (for me) a shocking non-standard way that events are fired
when using the System.EventArgs class, which apparently is manditory,
as is the "On" prefix for any member function that uses an "EventArgs"
type delegate. While I don't have time to discuss the "On" prefix in
this thread, suffice it to say that Eventhandler delegates used in a
class can (it's complicated) use a protected virtual method that fires
the event but must have prefix "On" as the name of the method. But
you can define a so-called non-generic EventHandler delegate that
avoids this, at the penalty of not conveying information back to any
class that is derived from EventArgs class. Source: C#3.0 Nutshell
by Albahari. It's really convoluted. They give an example but you
have to study it for like an hour, and like yoga, it finally comes to
you by osmosis. Also they show a cool way of using 'get' and 'set' to
fire an event, but that's kinda just eye candy.
But, I also discovered a shorthand way that delegates are chained, see
below. It's confusing when you first see this, so I am warning
newbies like myself.
Note these below lines are IDENTICAL!!!
this.buildProgress += BuildProgressHandler; //A1 //
BuildProgressHander is a function that returns void and takes
parameters (object, int)
//equivalent to (!)
this.buildProgress += new BuildProgress(BuildProgressHandler); //A2
// delegate void BuildProgress(object sender, int progressPercent); //
you can put this outside the class, i just repeat it here FYI
// this.BuildComplete += BuildCompleteHandler; //B1 //note format:
name of event on LHS and 'naked' format class method name on RHS!!!
//equivalent to (!)
this.BuildComplete += new EventHandler(BuildCompleteHandler); //B2
//note EventHandler delegate defined by .NET!!! You don't define it
yourself like delegate void BuildProgress above (EventArgs is the base
class)
To summarize: A1 works the same as A2 and B1 works the same as (is =
to) B2, even though to me the formats are radically different.
Shocking stuff dontyathink?
RL
discovered (for me) a shocking non-standard way that events are fired
when using the System.EventArgs class, which apparently is manditory,
as is the "On" prefix for any member function that uses an "EventArgs"
type delegate. While I don't have time to discuss the "On" prefix in
this thread, suffice it to say that Eventhandler delegates used in a
class can (it's complicated) use a protected virtual method that fires
the event but must have prefix "On" as the name of the method. But
you can define a so-called non-generic EventHandler delegate that
avoids this, at the penalty of not conveying information back to any
class that is derived from EventArgs class. Source: C#3.0 Nutshell
by Albahari. It's really convoluted. They give an example but you
have to study it for like an hour, and like yoga, it finally comes to
you by osmosis. Also they show a cool way of using 'get' and 'set' to
fire an event, but that's kinda just eye candy.
But, I also discovered a shorthand way that delegates are chained, see
below. It's confusing when you first see this, so I am warning
newbies like myself.
Note these below lines are IDENTICAL!!!
this.buildProgress += BuildProgressHandler; //A1 //
BuildProgressHander is a function that returns void and takes
parameters (object, int)
//equivalent to (!)
this.buildProgress += new BuildProgress(BuildProgressHandler); //A2
// delegate void BuildProgress(object sender, int progressPercent); //
you can put this outside the class, i just repeat it here FYI
// this.BuildComplete += BuildCompleteHandler; //B1 //note format:
name of event on LHS and 'naked' format class method name on RHS!!!
//equivalent to (!)
this.BuildComplete += new EventHandler(BuildCompleteHandler); //B2
//note EventHandler delegate defined by .NET!!! You don't define it
yourself like delegate void BuildProgress above (EventArgs is the base
class)
To summarize: A1 works the same as A2 and B1 works the same as (is =
to) B2, even though to me the formats are radically different.
Shocking stuff dontyathink?
RL