Pantonaut
Pantonaut

Reputation: 191

How can I provide a delegate in a managed interface class

I am working on a project containing parts of C#, managed C++ as well as unmanaged C++ code. I am working with .NET 3.5.

My programming experience lies more on the unmanaged C++ side an I am not that familiar with the concepts of .NET.

My question is: How can I hide / provide a delegate behind a managed interface class.

Suppose my interface looks sth. like this...

// I guess this is a type definition so it has to be placed outside of the interface
delegate void SignalStrengthDelegate(short input);

public interface class Test
{
    event SignalStrengthDelegate^ signalStrengthChanged;

    void doSomething();
};

... then I get an compiler-error in the ref class implementing this interface. The error suggests that i should implement methods for adding an removing the delegates. After trying to do this the compiler asks me to switch to /clr:oldSyntax... so that's also not what I am looking for.

I googled around a lot but can't find any suggestions for my problem... at some point I have the feeling that delegates and interfaces are some kind of contrary concepts and should not be mixed.

Any suggestions?

TIA!

Upvotes: 2

Views: 824

Answers (3)

Steven Sudit
Steven Sudit

Reputation: 19620

I dug up an article that seems to show the proper syntax: http://msdn.microsoft.com/en-us/magazine/cc163659.aspx

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502106

Well, you're declaring an event - and an event is basically an add/remove pair. That's why you're getting the compiler error. I suspect that you can do this, just not with the syntax you've been using. It would be very odd if you couldn't implement events in C++/CLI.

You may want to read my article on events, delegates and the differences between them.

What are you really trying to do? Do you want a property of the delegate type? Or a method which has the delegate type as a parameter? You say you want to "hide / provide a delegate" - what exactly do you mean?

Upvotes: 1

Related Questions