Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Doe - 31 Jul 2008 21:48 GMT
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);
Peter Duniho - 31 Jul 2008 22:29 GMT
> I have a C++/CLI assembly that exposes an event, e.g:
>
[quoted text clipped - 11 lines]
> TestClass obj = new TestClass();
> obj.someEvent += new EventHandler(someEventHandler);

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
John Doe - 31 Jul 2008 22:47 GMT
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.

> 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
[quoted text clipped - 5 lines]
>
> Pete
Peter Duniho - 31 Jul 2008 22:54 GMT
> Here's more info:
>
[quoted text clipped - 8 lines]
>  };
> }

Shouldn't that be:

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

???

Your original post included the "public:", but the above does not.  If you  
forgot that, well...that would certainly explain the error.  :)

Pete
John Doe - 31 Jul 2008 22:54 GMT
Nevermind, i noticed i didn't declare the event as public...

> Here's more info:
>
[quoted text clipped - 48 lines]
>>
>> Pete
Jon Skeet [C# MVP] - 31 Jul 2008 23:06 GMT
<snip>

> The compilation error is: ManagedEvents.Class1 does not contain a definition
> for 'someEvent' and no extension method 'someEvent' accepting a first
[quoted text clipped - 3 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
Web site: http://www.pobox.com/~skeet   
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.