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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Doe

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

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

    public ref class TestClass
    {
    public:
    event System::EventHa ndler^ 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(so meEventHandler) ;

  • Peter Duniho

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

    On Thu, 31 Jul 2008 13:48:14 -0700, John Doe <john@doe.comwr ote:
    I have a C++/CLI assembly that exposes an event, e.g:
    >
    public ref class TestClass
    {
    public:
    event System::EventHa ndler^ 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(so meEventHandler) ;
    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

    Comment

    • John Doe

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

      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 TestManagedCLIE vents
      {
      class Program
      {
      static void Main(string[] args)
      {
      Class1 testclass = new Class1();
      testclass.someE vent = new EventHandler(so meEventHandler) ;
      }

      private void someEventHandle r(Object ojb, EventArgs args)
      {

      }
      }
      }

      The compilation error is: ManagedEvents.C lass1 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" <NpOeStPeAdM@nn owslpianmk.comw rote in message
      news:op.ue6dy0a 48jd0ej@petes-computer.local. ..
      >
      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
      >

      Comment

      • Peter Duniho

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

        On Thu, 31 Jul 2008 14:47:33 -0700, John Doe <john@doe.comwr ote:
        Here's more info:
        >
        // ManagedEvents.h
        #pragma once
        using namespace System;
        namespace ManagedEvents {
        >
        public ref class Class1
        {
        event EventHandler^ someEvent;
        };
        }
        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

        Comment

        • John Doe

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

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


          "John Doe" <john@doe.comwr ote in message
          news:%23KXjNc18 IHA.356@TK2MSFT NGP02.phx.gbl.. .
          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 TestManagedCLIE vents
          {
          class Program
          {
          static void Main(string[] args)
          {
          Class1 testclass = new Class1();
          testclass.someE vent = new EventHandler(so meEventHandler) ;
          }
          >
          private void someEventHandle r(Object ojb, EventArgs args)
          {
          >
          }
          }
          }
          >
          The compilation error is: ManagedEvents.C lass1 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" <NpOeStPeAdM@nn owslpianmk.comw rote in message
          news:op.ue6dy0a 48jd0ej@petes-computer.local. ..
          >>
          >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
          >>
          >

          Comment

          • Jon Skeet [C# MVP]

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

            John Doe <john@doe.comwr ote:

            <snip>
            The compilation error is: ManagedEvents.C lass1 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.someE vent += new EventHandler(so meEventHandler) ;

            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.

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

            Comment

            Working...