Implements keyword & Events 
Author Message
 Implements keyword & Events

Hi

Can any1 help me with interface declaration and implements ?
I have interface declared in one dll. Part of this declarations are methods
and one event. Then I want implements this interface in one class. So I use
Implements keyword and I can implement methods. It's OK. But I want to raise
the event (declared inside interface). But I don't know how to do it. Has
any1 experiences with this ? Is it possible to do ?

  Thanx

      Petr



Sat, 22 Feb 2003 21:19:04 GMT  
 Implements keyword & Events

Events do not work with implements.  Instead, declare a separate interface
for the "event" method.  Then implement that interface in the receiver
class.  Initialize the sender object with a reference to the receiver.  When
you would normally raise the event, call the receiver through the "event"
interface.

Steven

Quote:

>Can any1 help me with interface declaration and implements ?
>I have interface declared in one dll. Part of this declarations are methods
>and one event. Then I want implements this interface in one class. So I use
>Implements keyword and I can implement methods. It's OK. But I want to
raise
>the event (declared inside interface). But I don't know how to do it. Has
>any1 experiences with this ? Is it possible to do ?



Sat, 22 Feb 2003 22:48:10 GMT  
 Implements keyword & Events

Thanx, good solution. But I've another problem with it. If I use Events, I
can turn off event handling in my class (set reference to Nothing), but I
don't want to destroy object (I have two references to object, one classical
and other as WithEvents and when I want to receive event I set the sedond
one). I can't do with interface. Don't you know how to solve this problem ?

  Peter



Quote:
> Events do not work with implements.  Instead, declare a separate interface
> for the "event" method.  Then implement that interface in the receiver
> class.  Initialize the sender object with a reference to the receiver.
When
> you would normally raise the event, call the receiver through the "event"
> interface.

> Steven


> >Can any1 help me with interface declaration and implements ?
> >I have interface declared in one dll. Part of this declarations are
methods
> >and one event. Then I want implements this interface in one class. So I
use
> >Implements keyword and I can implement methods. It's OK. But I want to
> raise
> >the event (declared inside interface). But I don't know how to do it. Has
> >any1 experiences with this ? Is it possible to do ?



Sat, 22 Feb 2003 23:37:39 GMT  
 Implements keyword & Events
Not sure I understand the problem exactly, but to the extent that I do, I
can think of several ways to accomplish what you want.  Perhaps the most
direct way is to do something like this:

For a simple "simulate events" scenario using interfaces, it's not uncommon
to allocate the receiver reference in the sender during the initialization
of the sender:

  Dim oSender As MySender
  Set oSender = New MySender
  oSender.Init oReceiver    ' Where oReceiver was allocated earlier

It's assumed that the Init method is some routine you define to work around
VB not supporting constructors.  Typically, an Init method is implemented
such that it can only be called once, because of that constructor semantic.
However, nothing says that the receiver cannot change.  Assume that instead
of Init, you have this:

  oSender.SetReceiver oReceiver

Then the code in oSender that notifies oReceiver merely needs to check
whether the member variable that hold oReceiver is set to something or not.
If you want to temporarily stop accepting notifications, call this method
with Nothing as the argument.

BTW, this all works better if you have a third "mediator" object controlling
the allocation of sender and receiver.  That way, neither has to directly
know about the other.  In effect, the native VB events are providing
something similar, albeit VB is the mediator.

Depending on your exact needs, you could do other things.  For example, the
receiver implement the notification interface in such a way that it could
react or not react to the events.  That is, the sender always sends them.

Steven

Quote:

>Thanx, good solution. But I've another problem with it. If I use Events, I
>can turn off event handling in my class (set reference to Nothing), but I
>don't want to destroy object (I have two references to object, one
classical
>and other as WithEvents and when I want to receive event I set the sedond
>one). I can't do with interface. Don't you know how to solve this problem ?



Sun, 23 Feb 2003 01:49:09 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. IDL->VB Implements & Events Question

2. IDL->VB Implements & Events Question

3. IDL->VB Implements & Events Question

4. implements & events

5. Implements keyword failing *sometimes*

6. Implements keyword

7. Implements Keyword in VBA

8. Doing AbstractFactory using VB's Implements Keyword?

9. Problems with polymorphism in VB5.0 when using keyword Implements

10. Critique of Implements keyword

11. 'Implements' keyword

12. VB6 does not maintain binary compatibility with VB5 projects that use Implements keyword

 

 
Powered by phpBB® Forum Software