By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,733 Members | 930 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,733 IT Pros & Developers. It's quick & easy.

Managed Event Question - Static Event

P: n/a
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

Mar 28 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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?
Mar 28 '07 #2

P: n/a

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


"Ben Voigt" wrote:
>
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?
Mar 29 '07 #3

P: n/a

"NEW2.NET" <NE*****@discussions.microsoft.comwrote in message
news:AE**********************************@microsof t.com...
>
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.
Mar 29 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.