How do I consume an event from C++/CLI in C#?

J

John Doe

I have a C++/CLI assembly that exposes an event, e.g:

public ref class TestClass
{
public:
event System::EventHandler^ someEvent;
}

My main C# project references this C++/CLI assembly, and I can see the
someEvent event in the Object Browser. However, when I tried to handle the
event, VS2008 complains that someEvent does not exist. Why is this so,
perhaps I did it wrong?

TestClass obj = new TestClass();
obj.someEvent += new EventHandler(someEventHandler);
 
J

John Doe

Here's more info:

// ManagedEvents.h
#pragma once
using namespace System;
namespace ManagedEvents {

public ref class Class1
{
event EventHandler^ someEvent;
};
}

Here's the main C# code that tries to handle someEvent:

using System;
using ManagedEvents;

namespace TestManagedCLIEvents
{
class Program
{
static void Main(string[] args)
{
Class1 testclass = new Class1();
testclass.someEvent = new EventHandler(someEventHandler);
}

private void someEventHandler(Object ojb, EventArgs args)
{

}
}
}

The compilation error is: ManagedEvents.Class1 does not contain a definition
for 'someEvent' and no extension method 'someEvent' accepting a first
argument of type 'ManagedEvents.Class1' could be found (are you missing a
using directive or an assembly reference?)

I've made sure the ManagedEvents C++/CLI assembly is referenced from the C#
project.
 
J

John Doe

Nevermind, i noticed i didn't declare the event as public...


John Doe said:
Here's more info:

// ManagedEvents.h
#pragma once
using namespace System;
namespace ManagedEvents {

public ref class Class1
{
event EventHandler^ someEvent;
};
}

Here's the main C# code that tries to handle someEvent:

using System;
using ManagedEvents;

namespace TestManagedCLIEvents
{
class Program
{
static void Main(string[] args)
{
Class1 testclass = new Class1();
testclass.someEvent = new EventHandler(someEventHandler);
}

private void someEventHandler(Object ojb, EventArgs args)
{

}
}
}

The compilation error is: ManagedEvents.Class1 does not contain a
definition for 'someEvent' and no extension method 'someEvent' accepting a
first argument of type 'ManagedEvents.Class1' could be found (are you
missing a using directive or an assembly reference?)

I've made sure the ManagedEvents C++/CLI assembly is referenced from the
C# project.

Peter Duniho said:
Do you have a concise-but-complete code sample that demonstrates the
error? What is the _exact_ text of the error? Do you have the C++/CLI
assembly properly referenced and included in a "using" directive in your
C# project?

What you posted looks fine. So it seems to me the problem must be in
some of the information you left out of your question.

Pete
 
J

Jon Skeet [C# MVP]

The compilation error is: ManagedEvents.Class1 does not contain a definition
for 'someEvent' and no extension method 'someEvent' accepting a first
argument of type 'ManagedEvents.Class1' could be found (are you missing a
using directive or an assembly reference?)

I've made sure the ManagedEvents C++/CLI assembly is referenced from the C#
project.

Well, apart from anything else you shouldn't be trying to *assign* to
the event - you should be trying to *subscribe to it:

testclass.someEvent += new EventHandler(someEventHandler);

However, if that were the only problem I'd expect a different error
message.

After a bit of experimenting, it seems that the problem is just that
the event isn't public. Make it public, change the code as above, and
it should be okay.
 

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