Solved

Events in class which Implements interface

Posted on 1998-09-17
9
333 Views
Last Modified: 2013-11-25
Preface:
-------
I am trying to create the abstract "parent" class and 10 "descendant" classes. Each Implements "ancestor" interface. In main form I need to create and use an instance of one of the 10 classes depending on the user's choice.
--------
Question:
--------
How to use events? I tried to declare events like that
Public Event Error(Msg As String)
in both abstract class (clsConverter) and descendant. But when I declare
Private WithEvents Converter As clsConverter
and try to create
Set Converter = New clsDescConverter
I got the error message with the following help
"You tried to use a WithEvents variable with a component that can't work as an event source for the specified set of events. For example, you may be sinking events of an object, then create another object that Implements the first object. Although you might think you could sink the events from the implemented object, that isn't automatically the case. Implements only implements an interface for methods and properties.

You can't sink events for a component that doesn't source events."
What could be the possible solution of this problem?
0
Comment
Question by:dksqrt
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 

Expert Comment

by:sjrl
ID: 1435406
I think you would have to have an object reference to the clsDescConverter in order to sink its events. This reference would have to be of the clsDescConverter type not the clsConverter type. The clsConverter doesn't know the interface of clsDescConverter and therefore cannot sink its events.

0
 

Author Comment

by:dksqrt
ID: 1435407
But having to declare all descendants WithEvents instances and sink is a nightmare!
I have 10 classes and 4 events I would have to write 10 declarations and 40 handlers! 40 handlers which do *the same* handling!
Any other ideas? I can reorganize code any way I want to but writing 10 copies of the same procedure - it's just disgusting from the OOP and even structured programming point of view...
What does "isn't automatically the case" in the previous help message mean? Perhaps some solution is possible?
And all my 4 events are *declared* in clsConverter and
clsDescConverter **Implements** clsConverter interface.
Why "The clsConverter doesn't know the interface of clsDescConverter and therefore cannot sink its events." ?
0
 

Expert Comment

by:sjrl
ID: 1435408
Have one class CMainClass which fires the events and does any computation common to call classes and has a property which is an IConverter object (ie class implementing the IConverter interface). You can sink events from this class. To allow for the difference in converters you set the IConverter parameter of the CMainClass to be clsDescConverter say. Then when CMainClass needs to do anything it can call the methods of the clsDescConverter class you passed it.

Suppose that you define a Convert method in the IConverter interface.
Give the CMainClass a property of type IConverter, call it converter.
Suppose you want to use the clsDescConverter. You set the CMainClass.Converter property to a clsDescConverter object.
Then when you call the Convert method of the CMainClass object, it would call the converter objects Convert method (which would be there because it is a part of the IConverter interface).

Hope this helps.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Expert Comment

by:sjrl
ID: 1435409
I've just realised I left an unanswered question.

clsDescConverter implements the clsConverter interface which is how it knows what that interface is. The clsConverter is the base object. It doesn't know anything about the clsDescConverter objects interface.
0
 

Author Comment

by:dksqrt
ID: 1435410
Yes it is unanswered
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1435411
There is not a solution to this problem.
This has to do with the way COM works and the way VB implements events. The events and the method are actually two different objects in COM.
With implements you implements only the method and the properties. You cannot inherited the events.
0
 

Author Comment

by:dksqrt
ID: 1435412
I got your point. But could you suggest any posiible workaround?
I have the pretty concrete task and I can design all involved classes anyway I want.
If it's not possible to have RaiseEvent-s in inherited classes - how to live without them?
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1435413
Pass the parent object to the interface.

clsConverter.Init me

and make the caller implement clsConverterCallback interface.
These are the event methods.

0
 

Accepted Solution

by:
prince_merlin earned 130 total points
ID: 1435414
You can implement the following workaround:

1. Create the parent object (Public Inherited As clsConverter) in each child. (in child's Class_Initialize())
2. Implement interface as usual without events.
3. Define events in the parent and create one additional procedure for each event in the parent class.

Public Sub RaiseMessage(Msg As String)
  RaiseEvent Message(Msg)
End Sub
4. Copy and paste these procedures in each child replacing the call inside to Call Inherited.RaiseMessage(Msg).
5. Replace all occurences of RaiseEvent Message(Msg)
in child classes to RaiseMessage(Msg)
6. Sink events in the form you need (such as the main form of a program) like this:

Private WithEvents GenConverter As clsConverter
.
Set Converter = New clsDescConverter
Set GenConverter = Converter.Inherited
'Ready to work - events will be sinked
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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