84 people like it.

Creating objects with events

This snippet shows how to create objects with events in F#. It shows both simple event (to be used from F#) and a .NET compatible event with specific delegate type.

Simple event for F# use

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
/// Counter with F#-only event
type SimpleCounter() =
  let evt = new Event<int>()
  let mutable count = 0
 
  /// Increments the counter and triggers 
  /// event after every 10 increments
  member x.Increment() =
    count <- count + 1
    if count % 10 = 0 then 
      evt.Trigger(count)

  /// Event triggered after every 10 increments
  /// The value carried by the event is 'int'
  member x.IncrementedTenTimes = evt.Publish 

.NET event compatible with C#

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
/// Derived EventArgs type that carries 'int' values
type IntEventArgs(count:int) = 
  inherit EventArgs()
  member this.Count = count

/// Standard EventHandler delegate for IntEventArgs
type IntEventHandler = delegate of obj * IntEventArgs -> unit

/// Counter with .NET compatible event
type DotNetCounter() =
  let evt = new Event<IntEventHandler, IntEventArgs>()
  let mutable count = 0
 
  /// Increments the counter and triggers 
  /// event after every 10 increments
  member x.Increment() =
    count <- count + 1
    if count % 10 = 0 then 
      evt.Trigger(x, IntEventArgs(count))

  /// Event triggered after every 10 increments
  /// (Creates standard .NET event using IntEventHandler delegate)
  [<CLIEvent>]
  member x.IncrementedTenTimes = evt.Publish 
Multiple items
type SimpleCounter =
  new : unit -> SimpleCounter
  member Increment : unit -> unit
  member IncrementedTenTimes : IEvent<int>

Full name: Script.SimpleCounter


 Counter with F#-only event


--------------------
new : unit -> SimpleCounter
val evt : Event<int>
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val mutable count : int
val x : SimpleCounter
member SimpleCounter.Increment : unit -> unit

Full name: Script.SimpleCounter.Increment


 Increments the counter and triggers
 event after every 10 increments
member Event.Trigger : arg:'T -> unit
member SimpleCounter.IncrementedTenTimes : IEvent<int>

Full name: Script.SimpleCounter.IncrementedTenTimes


 Event triggered after every 10 increments
 The value carried by the event is 'int'
property Event.Publish: IEvent<int>
Multiple items
type IntEventArgs =
  inherit EventArgs
  new : count:int -> IntEventArgs
  member Count : int

Full name: Script.IntEventArgs


 Derived EventArgs type that carries 'int' values


--------------------
new : count:int -> IntEventArgs
val count : int
Multiple items
type EventArgs =
  new : unit -> EventArgs
  static val Empty : EventArgs

Full name: System.EventArgs

--------------------
EventArgs() : unit
val this : IntEventArgs
member IntEventArgs.Count : int

Full name: Script.IntEventArgs.Count
type IntEventHandler =
  delegate of obj * IntEventArgs -> unit

Full name: Script.IntEventHandler


 Standard EventHandler delegate for IntEventArgs
type obj = Object

Full name: Microsoft.FSharp.Core.obj
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
Multiple items
type DotNetCounter =
  new : unit -> DotNetCounter
  member Increment : unit -> unit
  member add_IncrementedTenTimes : IntEventHandler -> unit
  member IncrementedTenTimes : IEvent<IntEventHandler,IntEventArgs>
  member remove_IncrementedTenTimes : IntEventHandler -> unit

Full name: Script.DotNetCounter


 Counter with .NET compatible event


--------------------
new : unit -> DotNetCounter
val evt : Event<IntEventHandler,IntEventArgs>
val x : DotNetCounter
member DotNetCounter.Increment : unit -> unit

Full name: Script.DotNetCounter.Increment


 Increments the counter and triggers
 event after every 10 increments
member Event.Trigger : sender:obj * args:'Args -> unit
Multiple items
type CLIEventAttribute =
  inherit Attribute
  new : unit -> CLIEventAttribute

Full name: Microsoft.FSharp.Core.CLIEventAttribute

--------------------
new : unit -> CLIEventAttribute
member DotNetCounter.IncrementedTenTimes : IEvent<IntEventHandler,IntEventArgs>

Full name: Script.DotNetCounter.IncrementedTenTimes


 Event triggered after every 10 increments
 (Creates standard .NET event using IntEventHandler delegate)
property Event.Publish: IEvent<IntEventHandler,IntEventArgs>
Raw view Test code New version

More information

Link:http://fssnip.net/1d
Posted:14 years ago
Author:Tomas Petricek
Tags: delegate , event