views:

51

answers:

3

Hello all,

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!

+1  A: 

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?

Jon Skeet
Jon, C#4 seems to handle an event in an interface fine. It just forces you to repeat it in the implementing classes.
Henk Holterman
@Henk: Yes, C# has always allowed you to specify an event in an interface. It's the implementation in C++ which is causing the OP grief, I believe.
Jon Skeet
Thank you! What I am trying to do is to provide an c++/cli-interface which hides the implementation from the C#-caller (the interface will be part of an API used by some other guys and I want to hide some methods from them that are used in the c++/cli-parts); in reference to the snipped above and assuming there is a class TestImpl that implements the interface Test, the C#-code should look sth. like this...Test test = new TestImpl();test.signalStrengthChanged +=new Test.SignalStrengthDelegate(signalStrengthChanged);
Superfisi
... so Jon, you are right... the implementation of the interface causes my problem; I just don't know how to satisfy the compiler. :)
Superfisi
@Superfisi: Unfortunately I don't know the C++/CLI syntax for it - but it looks like Ben's links will help you.
Jon Skeet
A: 

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

Steven Sudit
That's not C++/CLI. Please let Managed Extensions for C++ die the horrible death it deserves.
Ben Voigt
@Ben: Is http://stackoverflow.com/questions/462805/proper-way-of-raising-events-from-c-cli closer to the mark?
Steven Sudit
Yes, but it doesn't cover this question which is about abstract events.
Ben Voigt
@Ben: And it looks like you already posted a link to a good example of abstract events. At this point, I think it's probably best to upvote you and delete this answer, as it's redundant at best.
Steven Sudit