Implement vb.net interface event in managed C++

G

Guest

I have a VB.NET interface that my managed C++ code is to implement. I seem to
be stuck implementing an event defined in that interface. Does anyone have a
simple code snippet that will show me the basics of what I need to implement?
I've seen all the MSDN articles on implementing events in managed C++ and
I've gotten events to work without issue when implementing all the constructs
myself. I fail miserably when trying to correctly implement an event defined
in a VB.NET interface. It's not clear what's defined implicitly and
explicitly. I need your help.

Let's say we have a trivial VB.NET interface defined (my syntax may be off):

Public Interface ISomethingTrivial
Sub Func1()
Event Loaded()
End Interface

On the managed C++ side I would have:

__gc class MySomethingTrivial : public ISomethingTrivial
{
public:
// Constructor
// Destructor

void Func1() { /* Do something here */ }

// How implement the event here???
}

If I go ahead and implement the add_EventName() remove_EventName() events I
get errors about multiple methods being defined. So I get the gist that these
are created "behind the scenes" for me at compile time. I don't know how to
define the raise_EventName() appropriately as I don't know what else is under
the covers that I need to call. Each time I attempt to implement the event
(incorrectly) I can successfully compile my managed C++ class, but when I go
to instantiate an instance of it in my VB code I get "Cannot call New on a
class listed as MustOverride". I assume I will see this until I correctly
implement the event. Someone out there must have experience with this....

Many thanks for your help. - Brett
 
B

Brett Hall

After some more experimenting tonight I have the answer for the example
above.

__gc class MySomethingTrivial : public ISomethingTrivial
{
public:
// Constructor
// Destructor

void Func1()
{
// Do stuff
Fire_Loaded();
}

virtual __event LoadedEventHandler *Loaded;
void Fire_Loaded()
{
Loaded();
}
};

Many Thanks. Brett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top