Groups | Blog | Home
all groups > visual c > march 2007 >

visual c : Managed Event Question - Static Event


NEW2.NET
3/28/2007 2:28:10 PM
I have a static event declared in a C++ ref class, that can then be handled
in a VB app. I'm trying to expose the static event through the interface
that the C++ ref class implements so the VB app can use the interface as
preferred. How do you expose a static event through an interface?


In the example below as coded, in a VB app I can access and use the general
method (MyMethod) if I declare my handle variable as an IMyInterface type. I
can also use the general method and handle the event in VB if I declare as a
CMyClass. I cannot however seem to expose the static event through the
interface IMyInterface; any ideas?

public interface class IMyInterface
{
void MyMethod();

// delegate void EventHandler(const long lVal, bool% bVal); - WONT COMPILE
// static event EventHandler^ OnEventPost; - WONT COMPILE
};

public ref class CMyClass : IMyInterface
{
CMyClass();
~CMyClass();

virtual void MyMethod();

delegate void EventHandler(const long lVal, bool% bVal);
static event EventHandler^ OnEventPost;
};

Thanks
Ben Voigt
3/28/2007 5:11:21 PM

[quoted text, click to view]

Should work according to:
"An interface can contain declarations for functions, events, and
properties. All interface members have public accessibility. An interface
can also contain static data members, functions, events, and properties, and
these static members must be defined in the interface."

Note "must be defined in the interface", this probably prohibits forward
declaration.

[quoted text, click to view]

Have you tried without the const? Essentially const is only supported in
unmanaged code, managed types can only use literal and initonly, neither or
which are the same as const and neither of which can be applied to an
argument.

[quoted text, click to view]

What's the exact error message?

NEW2.NET
3/29/2007 5:24:02 AM

Thanks for your response and interest - I need the help. My mistake, it's
actually a link problem (i.e., not compile); the link errors are as follows:

Interface Assembly Link Errors:
MyInterface.obj : error LNK2020: unresolved token (0600003A)
IMyInterface::add_OnEventPost
MyInterface.obj : error LNK2020: unresolved token (0600003B)
IMyInterface::remove_OnEventPost

Implementing Ref Class Assembly Link Errors:
MyClass.obj : error LNK2020: unresolved token (0600003A)
IMyInterface::add_OnEventPost
MyClass.obj : error LNK2020: unresolved token (0600003B)
IMyInterface::remove_OnEventPost

Note the interface is in one assembly while implementing class is in another
assembly (as encouraged by interface programming guidance) thus the 2 sets of
link errors. Again this approach seems to work fine for all normal methods
and only has problems (i.e., link errors) dealing with events.

In reviewing these errors since our initial exchange, it's like I need to
define other 'stuff' just because I've now published in the interface. But
what other stuff (e.g., add, remove, raise) and what should it look like?
Again keep in mind all works fine if I use in VB directly refed as class
instead of interface.

I have tried eliminating the const specifier with no material change in
result.

Again thanks for any guidance here






[quoted text, click to view]
Ben Voigt
3/29/2007 6:04:08 PM

[quoted text, click to view]

Perhaps interface static events can't be defined as trivial events. See
"How to: Define Event Accessor Methods" in the Visual C++ documentation and
provide the event implementation explicitly.

AddThis Social Bookmark Button