?
Solved

How do I implement an event in an Interface?

Posted on 2003-02-25
4
Medium Priority
?
242 Views
Last Modified: 2010-04-17
Is it possible in VB6.0 to implement events in an interface? I've got an ActiveX dll with a base class and an inherited class (ITest and test_1). I want the inherited class to be able to raise an event that is then handled by an event handler on my test form.

The code below gives me a run-time error '459' Object or class does not support the set of events.

Help!

' ITest
Public Event MyEvent()

Public Function DoSomething()
'
End Function

' test_1
Implements ITest

Public Event MyEvent()

Private Function ITest_DoSomething() As Variant
  RaiseEvent MyEvent
End Function

' form1
Private WithEvents myI As Project1.ITest

Private Sub Command1_Click()
  Set myI = New Project1.test_1
  myI.DoSomething
 
End Sub

Private Sub myI_MyEvent()
  MsgBox "done"
End Sub
0
Comment
Question by:SIMONJ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Da_Weasel earned 200 total points
ID: 8018584
Grabbed this from the MSDN.

"Important   The Implements feature does not support outgoing interfaces. Thus, any events you declare in the class module will be ignored."

Looks like 'Implements' ignores any outbound interfaces.  Which mean you either have to create events in each class or you have to create your own event system if you want to use something like this.  This requires you using subclassing, and various hooks depending on what you want to do.

"Create events in each class" method (best for simple systems with out complex requirements):
------------
ITest Class:
------------
Public Sub DoSomething()
'
End Sub
-------------
test_1 Class:
-------------
Implements ITest
Public Event MyEvent()
Public Sub ITest_DoSomething()
    RaiseEvent MyEvent
End Sub
-----------
Form1 Form:
-----------
Public WithEvents myTest As test_1
Private Sub Form_Load()
    Set myTest = New test_1
    myTest.ITest_DoSomething
End Sub
Private Sub myTest_MyEvent()
    MsgBox "What are you looking at!"
End Sub

"Create your own event system" method (best for complex systems that need event and handling):

xen:OS by k23 Productions is a great example of this.  Its available at www.xen-os.net in source and binary.  If you want to see a project that uses xen:OS then goto www.cloud9ine.com, we are currently developing iceFireXL and Cloud9ine using xen:OS.  All of these are open source via the GNU General Public Licence.
0
 

Author Comment

by:SIMONJ
ID: 8024049
Thanks Da_Weasel, you confirmed my fear that this was not possible.

Wanted to write my own wrapper for different versions of Outlook.Application and I thought a common interface was the way to go, but if I can't handle the events with a common handler then I'm back to square one. I guess I could use callbacks instead...
0
 
LVL 3

Expert Comment

by:Da_Weasel
ID: 8025428
no a prob, good luck with that....
0
 

Author Comment

by:SIMONJ
ID: 8032185
decided to go with MAPI in the end rather than automating Outlook. I'm still using an interface, but not bothering to handle events. The send method simply returns true or false depending on whether the user cancels the mail or sends.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Progress

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question