delegates
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
delegates [2018/02/17 05:40] – created wolfgangriedmann | delegates [2018/07/11 09:36] (current) – [Why Delegates] wolfgangriedmann | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Delegates ====== | ====== Delegates ====== | ||
+ | |||
+ | ==== Why Delegates ==== | ||
Delegates are a new concept in the X# language - in VO they don't exist. | Delegates are a new concept in the X# language - in VO they don't exist. | ||
+ | |||
In every language there is sometimes the need to pass code as parameter to a function. VO has codeblocks, therefore normally you don't need function pointers, but pass codeblocks instead. | In every language there is sometimes the need to pass code as parameter to a function. VO has codeblocks, therefore normally you don't need function pointers, but pass codeblocks instead. | ||
+ | |||
The only real use of function pointers in the VO language is the interface to Windows API functions or other DLL calls. | The only real use of function pointers in the VO language is the interface to Windows API functions or other DLL calls. | ||
- | Since every pointer use can lead to serious problems when wrongly used, the .NET languages define another concept: the delegates. | + | |
+ | Since every pointer use can lead to serious problems when wrongly used, the .NET languages define another concept: the delegates. | ||
+ | |||
+ | ==== What is a Delegate ==== | ||
+ | |||
+ | A delegate is a declaration of a datatype that defines how a method must be defined, or better, a delegate defines the number and types of parameters and the type of return value. Sometimes this is referred as '' | ||
+ | |||
+ | Every datatype in .NET is really a class, and therefore also the delegate declaration is a (special) class (inherited from System.Delegate or System.MulticastDelegate), | ||
+ | |||
+ | And it looks like this: | ||
+ | < | ||
+ | The second piece that you normally need to use a delegate (like using a class), is one or more variables of the type of the delegate: | ||
+ | < | ||
+ | and then you can assign a method to the delegate: | ||
+ | < | ||
+ | oDelegate := Execute2 | ||
+ | oDelegate := Execute3 // Compile error here, the signature does not matches | ||
+ | |||
+ | method Execute1( nParm as int ) as string | ||
+ | return nParm: | ||
+ | |||
+ | method Execute2( nParm as int ) as string | ||
+ | return nParm: | ||
+ | |||
+ | method Execute3( cParm as string ) as string | ||
+ | return cParm</ | ||
+ | In most cases your work with the delegate will be finished here, as you pass the delegate variable to some other method. | ||
+ | |||
+ | But of course you can call the delegate also in your code: | ||
+ | < | ||
+ | oDelegate: | ||
+ | Of course you can omit also the Invoke() call - the compiler will add it for you: | ||
+ | < | ||
+ | |||
+ | Delegates can be also used in other cases: when you need to call a function in a dynamically loaded DLL. In VO we used the PCall() pseudo-function, | ||
+ | For a sample please look here: [[vo_to_net: | ||
+ | |||
+ | Please look also at this message in the X# forum by Chris Pyrgas: | ||
+ | [[https:// | ||
+ | |||
+ | ==== Further informations ==== | ||
+ | [[https:// | ||
+ | |||
+ | [[http:// | ||
+ | What is .Net Delegates ]] | ||
delegates.1518846036.txt.gz · Last modified: 2018/02/17 05:40 by wolfgangriedmann