| 1 | = Delegate Within Object Example = |
---|
| 2 | |
---|
| 3 | ''Part of'' DelegateCategory |
---|
| 4 | |
---|
| 5 | == Description == |
---|
| 6 | |
---|
| 7 | A simple example of how to use delegates within objects, as delegates are usually used for GUI event handling such as in Delphi or C#. |
---|
| 8 | |
---|
| 9 | == Example == |
---|
| 10 | |
---|
| 11 | {{{ |
---|
| 12 | #!d |
---|
| 13 | // Simple sample: how to use delegates within objects |
---|
| 14 | |
---|
| 15 | import std.c.stdio; |
---|
| 16 | |
---|
| 17 | // DelegateClass: a class with declared delegate function "printText" |
---|
| 18 | class DelegateClass |
---|
| 19 | { |
---|
| 20 | public void delegate(char[] ) printText; |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | int main() |
---|
| 24 | { |
---|
| 25 | // Function print |
---|
| 26 | void print(char[] text) { printf(text); } |
---|
| 27 | |
---|
| 28 | // Initialize a DelegateClass |
---|
| 29 | DelegateClass delclass = new DelegateClass(); |
---|
| 30 | |
---|
| 31 | // Assign the delegate "printText" to function "print" |
---|
| 32 | delclass.printText = &print; |
---|
| 33 | |
---|
| 34 | // Call printText to test |
---|
| 35 | |
---|
| 36 | delclass.printText("HELLO\n"); |
---|
| 37 | |
---|
| 38 | return 0; |
---|
| 39 | } |
---|
| 40 | }}} |
---|
| 41 | |
---|
| 42 | == Source == |
---|
| 43 | |
---|
| 44 | || Link || http://www.dsource.org/tutorials/index.php?show_example=102 || |
---|
| 45 | || Posted by || Anonymous || |
---|
| 46 | || Date/Time || Fri Jun 25, 2004 2:49 am || |