Show/Hide Toolbars

XSharp

Navigation: X# Examples

Anonymous Method Expressions

Scroll Prev Top Next More

An example of an Anonymous Method Expression (AME) (note the DELEGATE keyword):
Note that the body of the that you can have :

1.A single Expression

2.An Expression List

3.A statement List

The first 2 require the expression(s) to be on the same line as the opening Curly { . Of course you can use the statement continuation character ; to tell the compiler that you have spread the statement over more than one line.
The last one requires the statements in the list to be on separate lines and the closing Curly } must also be on a separate line. This is shown in the example below.

 

USING System.Windows.Forms
FUNCTION Start() AS VOID  
  TestAnonymous()
  RETURN
 
FUNCTION TestAnonymous() AS VOID
  LOCAL oForm AS Form
  oForm := Form{}
  oForm:Text := "Click me to activate the anonymous method"
  oForm:Click += DELEGATE(o AS System.Object, e AS System.EventArgs ) {  
                 System.Windows.Forms.MessageBox.Show("Click 1!")    
                 System.Windows.Forms.MessageBox.Show("Click 2!")  
              }  
  oForm:ShowDialog()
  RETURN