Reputation: 345
I need IMessage Interface. In C# I could write
CDO.Message oMsg = new CDO.Message(); // ok
but when I try to do this in F#
let oMsg = new CDO.Message()
I get an error message:
'new' cannot be used on interface types. Consider using an object expression '{ new ... with ... }' instead.
How to solve this problem?
Upvotes: 8
Views: 895
Reputation: 28499
You need to use the version that has the "Class" added to the interface name:
open System
[<EntryPoint>]
[<STAThread>]
let main argv =
let oMsg = new CDO.MessageClass()
oMsg.Subject <- "Hello World"
Console.WriteLine(oMsg.Subject)
Console.ReadLine() |> ignore
0
Note: I also explicitly had to add a reference to Microsoft ActiveX Data Objects 6.0 Library
to the project to make the object instantiation work.
In case you want to use the specific interface and not the Co-Class (as proposed by Hans Passant), use this version that casts the created Co-Class to the interface, thereby returning a pointer to the actual interface.
open System
[<EntryPoint>]
[<STAThread>]
let main argv =
let oMsg = upcast new CDO.MessageClass() : CDO.IMessage
oMsg.Subject <- "Hello World"
Console.WriteLine(oMsg.Subject)
Console.ReadLine() |> ignore
0
Upvotes: 11