User Tools

Site Tools


delegates

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
delegates [2018/02/17 14:38] wolfgangriedmanndelegates [2018/07/11 09:36] (current) – [Why Delegates] wolfgangriedmann
Line 4: Line 4:
  
 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. And therefore you should also use delegates when you need to dynamically call functions. Please see below in the PCall note for a link to a sample.
  
 ==== What is a Delegate ==== ==== 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 signature of a method (a signature is the number and types of parameters and the type of return value).  +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 ''signature'', but the definitions of this term are not clear: sometimes the return value is part of the signature, sometimes not. 
-Every datatype in .NET is really a class, and therefore also the delegate declaration is a (special) class, that normally is defined outside of a class, but since .NET permits nested classes (a class that is visible only in another class), a delegate can also be declared inside a class declaration.+ 
 +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), that normally is defined outside of a class, but since .NET permits nested classes (a class that is visible only in another class), a delegate can also be declared inside a class declaration. 
 And it looks like this: And it looks like this:
 <code>delegate IntDelegate( nParm as int ) as string</code> <code>delegate IntDelegate( nParm as int ) as string</code>
Line 30: Line 35:
   return cParm</code>   return cParm</code>
 In most cases your work with the delegate will be finished here, as you pass the delegate variable to some other method. 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: But of course you can call the delegate also in your code:
 <code>oDelegate:Invoke( 1234 ) <code>oDelegate:Invoke( 1234 )
Line 35: Line 41:
 Of course you can omit also the Invoke() call - the compiler will add it for you: Of course you can omit also the Invoke() call - the compiler will add it for you:
 <code>oDelegate( 789 )</code> <code>oDelegate( 789 )</code>
 +
 +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, in Vulcan also PCallNative().
 +For a sample please look here:  [[vo_to_net:pcall|PCall vs PCallNative]]
 +
 +Please look also at this message in the X# forum by Chris Pyrgas:
 +[[https://www.xsharp.info/forum/public-chit-chat/542-the-x-documentation-project?start=70#4064|https://www.xsharp.info/forum/public-chit-chat/542-the-x-documentation-project?start=70#4064]]
 +
 ==== Further informations ==== ==== Further informations ====
 [[https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/|Delegates (C# Programming Guide)]] [[https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/|Delegates (C# Programming Guide)]]
 +
 [[http://net-informations.com/faq/net/delegates.htm| [[http://net-informations.com/faq/net/delegates.htm|
 What is .Net Delegates ]] What is .Net Delegates ]]
delegates.1518878324.txt.gz · Last modified: 2018/02/17 14:38 by wolfgangriedmann