tags:

views:

406

answers:

1

Hi!

I am trying to publish an event from an F# type, but I want it to be seen as an event from C# or VB. It seems that the correct way to do it used to be IEvent.create_HandlerEvent, but this function doesn't exist in the newest version of F#. So what is the correct way to do it now?

+10  A: 

Events are not my forte, but this example seems to work on F# 1.9.6.16:

namespace EventExample
open System
type MyEventArgs(msg:string) =
    inherit EventArgs()
    member this.Message = msg

type MyEventDelegate = delegate of obj * MyEventArgs -> unit

type Foo() = 
    let ev = new Event<MyEventDelegate, MyEventArgs>()

    member this.Ping(msg) =
        ev.Trigger(this, new MyEventArgs(msg))

    [<CLIEvent>]
    member this.GotPinged = ev.Publish

See also

http://cs.hubfs.net/forums/thread/10555.aspx

Brian
Thanks, it works :) May I ask how did you know about the CLIEventAttribute? I don't see it mentioned anywhere in the documentation/language specification/google...
Michał Bendowski
I know because I'm a developer on the F# team, so I have 'inside information'. :) Yeah, this seems to be missing from the release notes and the spec; we'll be doing a spec update soon.(Feel free to give me an upvote in addition to the check mark. :) )
Brian
Gotta love it when the insiders appear with the goods ;-p
Marc Gravell
What happened to the IEvent interface described in Expert F#?
Ben Collins
We've iterated on the event design a couple times since the book was published... if there's an example you have trouble translating to the newer APIs, ask.
Brian
+1 this was more useful than MSDN!. Any plans for Don (or anyone else) to release an updated version of Expert f#? It previously was my bible for doing things but sadly I'm hitting unfortunate brick walls with parts that have changed in the ...9.16 release
ShuggyCoUk
Don et al. are working on an update to Expert F# currently, yes.
Brian