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

  • Thread starter Thread starter John Doe
  • Start date Start date
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);
 
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.
 
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
 
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.
 
Back
Top