> You didn't disclose the declaration of MyErrorDelegate, but it looks
> like it's not compatible with the handler. Your event handler must have
> the exact same arguments and return type as the delegate. For example,
> MCDelegate is declared
Opps a typo on my part the delegate was declared, and the event is declared
below, and it has the same values, so I don't understand how it cannot
compile.
Especially since the c# works fine?
// Here is the Delegate:
public delegate bool MCDelegate(System::String^ errorCode);
public ref class MCEventTesting
{
public:
MCEventTesting(void);
event MCDelegate^ SyncEvent;
event MCDelegate^ AsyncEvent; // Async Event?????
public:
// Fire the event as an async event.
void fireErrorEventAsync(System::String^ errorCode)
{
// ERROR: "Complains that it must be a pointer to a member function."
MCDelegate^ delg = gcnew MCDelegate(this,&MCEventTesting::AsyncEvent);
AsyncEvent->BeginInvoke("test",nullptr,this);
// Also tried this, it didn't work complained that it needs to be a data
member.
// MCEventTesting::AsyncEvent->BeginInvoke(errorCode,nullptr,nullptr);
}
// Works fine.
void fireSyncEvent(System::String^ errorCode)
{
SyncEvent(errorCode);
}
};
Ben Voigt - 07 Nov 2006 20:01 GMT
>> You didn't disclose the declaration of MyErrorDelegate, but it looks
>> like it's not compatible with the handler. Your event handler must have
[quoted text clipped - 29 lines]
> member.
> // MCEventTesting::AsyncEvent->BeginInvoke(errorCode,nullptr,nullptr);
Use this along with a custom event:
private:
MCDelegate^ AsyncHandlerList;
public:
event MCDelegate^ Async {
MCDelegate^ add(MCDelegate^ d) { return AsyncHandlerList =
Delegate::Combine(AsyncHandlerList, d); }
MCDelegate^ remove(MCDelegate^ d) { return AsyncHandlerList =
Delegate::Remove(AsyncHandlerList, d); }
}
void fireErrorEventAsync(System::String^ errorCode)
MCEventTesting::AsyncHandlerList->BeginInvoke(errorCode,nullptr,nullptr);
}
> // Works fine.
> void fireSyncEvent(System::String^ errorCode)
> {
> SyncEvent(errorCode);
> }
> };
BartMan - 07 Nov 2006 20:19 GMT
> event MCDelegate^ Async {
> MCDelegate^ add(MCDelegate^ d) { return AsyncHandlerList =
> Delegate::Combine(AsyncHandlerList, d); }
> MCDelegate^ remove(MCDelegate^ d) { return AsyncHandlerList =
> Delegate::Remove(AsyncHandlerList, d); }
> }
Hello Ben,
Thanks for the reply. The above mentioned code issues the following error:
Error 1 error C3919: 'Async::add': function must have type 'void (MCDelegate
^)'
I tried changing it to "void add(MCDelegate^ d) {....}", but that didn't
seem to work either.
Ben Voigt - 07 Nov 2006 20:23 GMT
>> event MCDelegate^ Async {
>> MCDelegate^ add(MCDelegate^ d) { return AsyncHandlerList =
[quoted text clipped - 12 lines]
> I tried changing it to "void add(MCDelegate^ d) {....}", but that didn't
> seem to work either.
also take out the word return. Oh, and the += and -= operators do work in
C++. Here's a snippet out of my own code:
virtual event System::EventHandler^ Closed
{
void add( System::EventHandler^ handler )
{
CloseHandlers += handler;
}
void remove( System::EventHandler^ handler )
{
CloseHandlers += handler;
}
}
BartMan - 07 Nov 2006 20:33 GMT
> also take out the word return. Oh, and the += and -= operators do work in
> C++. Here's a snippet out of my own code:
[quoted text clipped - 20 lines]
>
> }
Thanks that is very good to know. I did not know that operator support has
been added in c++/clr. Thank for all your help, it is exactly what I needed.
Here is my final version which seems to execute properly.
public delegate bool MCDelegate(System::String^ errorCode);
public ref class MCEventTesting
{
public:
MCEventTesting(void);
event MCDelegate^ SyncEvent;
private:
MCDelegate^ AsyncHandlerList;
public:
event MCDelegate^ Async
{
void add(MCDelegate^ d)
{
AsyncHandlerList =
(MCDelegate^)System::Delegate::Combine(AsyncHandlerList, d);
}
void remove(MCDelegate^ d)
{
AsyncHandlerList = (MCDelegate^)System::Delegate::Remove(AsyncHandlerList,
d);
}
}
public:
void fireErrorEventAsync(System::String^ errorCode)
{
MCEventTesting::AsyncHandlerList->BeginInvoke(errorCode,nullptr,nullptr);
}
void fireSyncEvent(System::String^ errorCode)
{
SyncEvent(errorCode);
}
};
Ben Voigt - 07 Nov 2006 20:43 GMT
> Thanks that is very good to know. I did not know that operator support
> has
[quoted text clipped - 23 lines]
> AsyncHandlerList =
> (MCDelegate^)System::Delegate::Combine(AsyncHandlerList, d);
As I showed you with my code, this can be shortened to: AsyncHandlerList +=
d
> }
>
> void remove(MCDelegate^ d)
> {
> AsyncHandlerList = (MCDelegate^)System::Delegate::Remove(AsyncHandlerList,
> d);
And here, AsyncHandlerList -= d
> }
> }
[quoted text clipped - 3 lines]
> {
> MCEventTesting::AsyncHandlerList->BeginInvoke(errorCode,nullptr,nullptr);
Don't forget to get the IAsyncResult and call EndInvoke later. And yes,
this is a pain, but it's required. You can pass an anonymous method as the
callback to BeginInvoke to help with this.
> }
>
[quoted text clipped - 3 lines]
> }
> };