If I am not mistaken, Event is just a keyword that modifies delegates so they appear in the property explorer and allow the compiler to perform consumption and chaining(is that the right term?)
delegates are simply a user defined type that is the format for a method -
At the risk of being censored
, I am going to use delphi syntax here because it really drives the concept home:
in delphi you can do the following
Code:
type
TMyMethod = procedure(sender: TObject);
var
MyMethod: TMyMethod;
procedure Method1(sender: TObject);
begin
// do domething with sender
end;
procedure Method2(sender: TObject);
begin
// do domething with sender
end;
begin
if [some runtime specified boolean value such as a check box] then
MyMethod = Method1
else
MyMethod = Method2;
MyMethod(Application);
end.
So, TMyMethod is a definition delegating any function that takes a single argument of type TObject;
I declare a variable of type TMyMethod,
MyMethod .
I declare two methods that take a single argument of type TObject, making them eligable for delegation by a variable of type TMyMethod.
at run time, the program does some dynamic analysis and assigns the appropriate method to MyMethod and then executes the method using the application variable as the argument.
now. . . open mind time. . . use your imagination. . .
arrays can contain any type of data and can be loaded dynamically at runtime. . .
consider an array of delegates, the actual methods being delegated are specified at run time depending on the user specified option selections. . .
After iterating through the array and executing the user specfied methods, the user can select a set of different options. the program loads the new delegated methods based on the selections and the program executes with a completely different behavior.
Again, I highly suggest that anyone new to OOP download delphi. not because delphi is the end all be all of programming languages but because delphi comes with vitually ALL the source used to devlop the delphi components. It is a wonderful source for ideas and examples of real world OOP technique that transcends syntax.
Oh yeah, and its type library editor is phenomenal!
Joe Mamma. . . free your mind - vote Kerry!!!