Export (0) Print
Expand All
Expand Minimize

Compiler Error C3825

'class': a managed class can only support managed events

Only .NET events are supported in managed classes. To fix this error, change type parameter of event_source and event_receiver from native to managed or remove the attribute.

The following sample generates C3825:

// C3825a.cpp
// compile with: /clr
public delegate void del1();

[event_source(native)]           // To fix, change 'native' to 'managed' or delete this line
ref class CEventSrc
{
public:
   event del1^ event1;       // C3825

   void FireEvents() {
      event1();
   }
};

[event_receiver(native)]         // To fix, change 'native' to 'managed' or delete this line
ref class CEventRec
{
public:
   void handler1()
   {
      System::Console::WriteLine("Executing handler1().\n");
   }
   void HookEvents(CEventSrc^ pSrc) 
   {
      pSrc->event1 += gcnew del1(this, &CEventRec::handler1);
   }
   void UnhookEvents(CEventSrc^ pSrc) 
   {
      pSrc->event1 -= gcnew del1(this, &CEventRec::handler1);
   }
};

int main() 
{
   CEventSrc^ pEventSrc = gcnew CEventSrc;
   CEventRec^ pEventRec = gcnew CEventRec;
   pEventRec->HookEvents(pEventSrc);
   pEventSrc->FireEvents();
   pEventRec->UnhookEvents(pEventSrc);
}

For Managed Extensions for C++ You can also remove the __gc from the class declarations so that they are not garbage-collected classes.

The following sample generates C3825:

// C3825b.cpp
// compile with: /clr:oldSyntax
[event_source(native)]           // To fix, change 'native' to 'managed'
                                 // or, remove __gc from class
__gc class CEventSrc
{
public:
    __event void event1();       // C3825

    void FireEvents() {
       event1();
    }
};

[event_receiver(native)]         // To fix, change 'native' to 'managed'
                                 // or, remove __gc from class
__gc class CEventRec
{
public:
    void handler1()
    {
        System::Console::WriteLine("Executing handler1().\n");
    }
    void HookEvents(CEventSrc* pSrc) 
    {
        __hook(&CEventSrc::event1, pSrc, &CEventRec::handler1);
     }
    void UnhookEvents(CEventSrc* pSrc) 
    {
        __unhook(&CEventSrc::event1, pSrc, &CEventRec::handler1);
    }
};

int main() 
{
    CEventSrc* pEventSrc = new CEventSrc;
    CEventRec* pEventRec = new CEventRec;
    pEventRec->HookEvents(pEventSrc);
    pEventSrc->FireEvents();
    pEventRec->UnhookEvents(pEventSrc);
}
Show:
© 2014 Microsoft