Results 1 to 4 of 4

Thread: Implements WithEvents

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    I'm trying to work out how to use the Implements keyword properly, And I can't Get it to work If i'm using Events.

    Say I have a class clsInterface

    Code:
    Event MyEvent()
    
    
    
    Public Property Get MyProperty As Long
    
    End Property
    
    Public Property Let MyProperty (New_MyProperty As Long)
    
    End Property
    
    Public Sub MyMethod()
    
    End Sub


    Now I want several Different classes (say cls1,cls2 and cls3) to Implement this interface so I can do something like this

    Code:
    Dim WithEvents objInstance As  clsInterface
    
    Private Sub Form_Load
    
    Select Case Fix(Rnd * 3)
    
        Case 0
            Set objInstance = New cls1
    
        Case 1
            Set objInstance = New cls2
    
        Case 2
            Set objInstance = New cls3
    
    End Select
    
    End Sub
    And of coure some other code to make it do something useful

    When I try to do this I always get an "Object Or Class does Not Support the set of Events" Error, no matter what events I try to put in cls1, cls2 and cls3.

    Can anyone give me an example of a class that Implements clsInterface properly and Supports the Set of Events.

    Thanks in Advance.

  2. #2
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Hi,
    Using implements, your interface that you are implementing cannot have events.
    From MSDN
    Implements only implements an interface for methods and properties.
    So...
    You will have to put your events in each class yourself that implements your interface.

    eg.
    Code:
    'Interface named Iinterface
    Option Explicit
    
    Public Function GetData() As String
    'No code goes here as this is purely an interface
    End Function
    
    Public Sub LoadData()
    'No code goes here as this is purely an interface
    End Sub
    
    
    
    'Class named clsData
    Option Explicit
    
    Implements Iinterface
    Public Event DataChanged() 'put your event in this class
    
    Private Function iinterface_GetData() As String
    'Code goes here to get data
    End Function
    
    Private Sub iinterface_LoadData()
    'Code goes here to load data
    End Sub
    
    
    'And then in your form:
    Option Explicit
    
    Dim WithEvents objObject As clsdata
    
    Private Sub Form_Load()
       Set objObject = New clsdata
    End Sub
    
    Private Sub objObject_DataChanged()
       'Has its own event
    End Sub
    This probably hasn't helped in any way but hey..it might do!

    Shaun

    P.S. I am just getting to grips with this stuff so if anybody does have a solution...feel free to post it
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Not really what I wanted to hear, but thanks anyway, Unfortunatley I can't use your code in this example because I need to use several different class modules with the same Interface and raising vents from them, so I have to declare them all as a single type of object. (Which is where I thought the implements keyword would be really handy as I'm pretty sure that's what it was designed for.)


    Does anyone else know a good way around this?

  4. #4
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Hi Sam,
    I have been looking around (mainly in the new book I had delivered today!!) and you use the Implements keyword to keep your objects interfaces consistent but this definately only applies to properties and methods.

    The original class that you use as the interface does not have any code in, it is only a template for others and as such cannot have events in it.

    Therefore, for every class that you create that implements your interface you will have to define the events within them each time.

    This is fine if you didn't want to declare them all as an interface object (in your case clsInterface), but you can't do this because an implemented interface simply does not support events and there is no way round this, sorry.

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

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