Results 1 to 10 of 10

Thread: Events and Implements

  1. #1
    Guest
    Is it possible to use events when useing Implements

    Say for example i have an interface for Animals
    Some basic functions in it like eat,sleep etc
    Now i want some events for the animals like, Tired/Angry etc

    Now i have a class called Dog
    it implements Animal so i can use the functions and sub etc, but now i want the dog to raise the events that it is tired, however it seems i cannont get the event to work.

    When i add the event only to the Interface(Animal) then

    Private Withevents CAnminal as IAnimal
    Set CAnimal = New CDog

    i get an error "Class or object does not support the set of events."
    Same thing happens if i just copy the event to the Dog class


    Is there anyway of doing this? (slimey hacks are welcome =])

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Je zou kunnen proberen om de add-in die een activex control kan maken te gebruiken. Die kan ook events maken.

  3. #3
    Guest
    Originally posted by oetje
    Je zou kunnen proberen om de add-in die een activex control kan maken te gebruiken. Die kan ook events maken.
    I have no need for a usercontrol, i want to be able to raise the events from the class, but the class wont let me raise events because i dont know how i can implement Events(or if its even possible).

    PS. Post in English as more people might have use for your awnser, want to read it, dont understand Dutch

  4. #4
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    I wanted to tell that you can create a dummy user control to see how the events work.

  5. #5
    Guest
    I know how events work.

    Its just that i cant raise events from classes that are implementations of my interface.

    I want to define events in my interface and then be able to let the implementation of the interface raise an event.

    I can raise an event from a normal class but not from an implementation because of the "Object or class does not support set of events." error

  6. #6
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Ok, I thought you didn't know how to use the RaiseEvent stuff.

  7. #7
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Hi,
    You cannot have events in the actual interface itself. There is no work around for this, it is just not possible.

    You can however, have events in the class that implements the interface.

    Hope this helps

    Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  8. #8
    Guest
    Yes, but if i wanted to raise events from the class, i have to declare it as itself and that would throw away the interface.

    I guess ill just have to check a state in the class instead of raising an event. To bad this will make the code that much more unreadable.

  9. #9
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    OK, There Is a workaround for this.

    Add 2 new Classes to your Project, a CAnimal and a CEventHandler

    The Event Handler is a very Simle class used to Pass Events through from the Implemented Class

    So In your CDog Add a Property Get Like this


    Code:
    Dim MyEvents as New CEventHandler
    
    Public Property Get EventHandler() As CEventHandler
        Set EventHandler = MyEvents
    End Property

    And Change all your RaiseEvent Angrys to Call Me.EventHandler.RaiseAngry and the same For RaiseEvent Tired.


    Then Make a Wrapper Class For it all callse CAnimal


    Code:
    Dim MyAnimal As IAnimal
    Dim WithEvents AnimalEvents As CEventHandler
    
    
    'Make this the Default Property
    'This way to make a new CDog We Just do
    'Dim WithEvents Fido As CAnimal
    'Set Fido = New CAnimal
    'Fido = New CDog
    'We Make it a Property Let so we can distinguish betwen Setting the Wrapper Class and Setting the Animal Property
    Public Property Let Animal(New_Animal As IAnimal)
        Set MyAnimal = New_Animal
    End Property
    
    
    Private Sub AnimalEvents_Tired()
        RaiseEvent Tired
    End Sub
    
    Private Sub AnimalEvents_Angry()
        RaiseEvent Tired
    End Sub
    
    
    'Put Your Properties And Methods Here And Pass them through to the MyAnimal Object

    Then You Can Use the CAnimal Class and all its events By Setting it's Animal Property to whateer you like

  10. #10
    Guest
    Yes i was thinking about that as well.
    Thanks for the help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width