Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2007

Tip: Looking for answers? Try searching our database.

Managed Event Question - Static Event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
NEW2.NET - 28 Mar 2007 22:28 GMT
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 - 28 Mar 2007 23:11 GMT
> In the example below as coded, in a VB app I can access and use the
> general
[quoted text clipped - 4 lines]
> 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?
NEW2.NET - 29 Mar 2007 13:24 GMT
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

> > In the example below as coded, in a VB app I can access and use the
> > general
[quoted text clipped - 30 lines]
>
> What's the exact error message?
Ben Voigt - 30 Mar 2007 00:04 GMT
> 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
[quoted text clipped - 19 lines]
> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.