Show/Hide Toolbars

XSharp

XSharp now supports an extended Event syntax. Both a single line syntax is supported as a multi line:

Old style syntax

 

[Attributes] [Modifiers] EVENT [<ExplicitInterface>.] <Id> AS <Type>    // Old Style

Single Line syntax with explicit expressions

 

[Attributes] [Modifiers] EVENT [<ExplicitInterface>.] <Id> AS <Type>   [ADD <ExpressionList] [REMOVE <ExpressionList]        // Single Line

Multi line syntax with explicit expressions

[Attributes] [Modifiers] EVENT [<ExplicitInterface>.] <Id> AS <Type>     // Multi line
 
ADD
   <Statements>
END [ADD]
REMOVE
   <Statements>
END [REMOVE]
END [EVENT]

 

Example

USING System.Collections.Generic
FUNCTION Start AS VOID
  LOCAL e AS EventsExample
   e := EventsExample{}
   e:Event1 += TestClass.DelegateMethod
   e:Event1 += TestClass.DelegateMethod
   e:Event1 -= TestClass.DelegateMethod   // added 2, removed 1, should be called once
   e:Event2 += TestClass.DelegateMethod
   e:Event2 += TestClass.DelegateMethod
   e:Event2 -= TestClass.DelegateMethod // added 2, removed 1, should be called once
   e:Event3 += TestClass.DelegateMethod
   e:RaiseEvent1("This is a test through a multi line event definition")
   e:RaiseEvent2("This is a test through a single line event definition")
   e:RaiseEvent3("This is a test through an old style event definition")
   Console.WriteLine("Press a Key")
   Console.ReadLine()
 
 
DELEGATE EventHandler (s AS STRING) AS VOID
 
CLASS TestClass
  STATIC METHOD DelegateMethod(s AS STRING ) AS VOID
       Console.WriteLine( s)
END CLASS
 
 
CLASS EventsExample
  PRIVATE eventsTable AS Dictionary<STRING, System.Delegate>
  PRIVATE CONST sEvent1 := "Event1" AS STRING
  PRIVATE CONST sEvent2 := "Event2" AS STRING
   CONSTRUCTOR()
       eventsTable := Dictionary<STRING, System.Delegate>{}
       eventsTable:Add(sEvent1,NULL_OBJECT)
       eventsTable:Add(sEvent2,NULL_OBJECT)
 
  // Multiline definition
  EVENT Event1 AS EventHandler
       ADD
          BEGIN LOCK eventsTable
               eventsTable[sEvent1] := ((EventHandler) eventsTable[sEvent1]) + value
          END LOCK
           Console.WriteLine(__ENTITY__ + " "+value:ToString())
      END
       REMOVE
          BEGIN LOCK eventsTable
               eventsTable[sEvent1] := ((EventHandler) eventsTable[sEvent1]) - value
          END LOCK
           Console.WriteLine(__ENTITY__+ " "+value:ToString())
      END
  END EVENT
 
 
  // Single Line defintion on multilpe lines with semi colons, for better reading !
  EVENT Event2 AS EventHandler  ;
       ADD     eventsTable[sEvent2] := ((EventHandler) eventsTable[sEvent2]) + value ;
       REMOVE  eventsTable[sEvent2] := ((EventHandler) eventsTable[sEvent2]) - value
 
  // Old style definition
  EVENT Event3 AS EventHandler
 
  METHOD RaiseEvent1(s AS STRING) AS VOID
      VAR handler := (EventHandler) eventsTable[sEvent1]
      IF handler != NULL
           handler(s)
      ENDIF
 
  METHOD RaiseEvent2(s AS STRING) AS VOID
      VAR handler := (EventHandler) eventsTable[sEvent2]
      IF handler != NULL
           handler(s)
      ENDIF
 
  METHOD RaiseEvent3(s AS STRING) AS VOID
      IF SELF:Event3 != NULL
           Event3(s)
      ENDIF
END CLASS