Managed Event Question - Static Event

G

Guest

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
 
B

Ben Voigt

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?

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.
public interface class IMyInterface
{
void MyMethod();

// delegate void EventHandler(const long lVal, bool% bVal); - WONT
COMPILE

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.
// static event EventHandler^ OnEventPost; - WONT COMPILE
};

What's the exact error message?
 
G

Guest

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
 
B

Ben Voigt

NEW2.NET said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top