Microsoft KB Archive/172329

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 10:05, 21 July 2020 by X010 (talk | contribs) (Text replacement - """ to """)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Article ID: 172329

Article Last Modified on 5/13/2003



APPLIES TO

  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 5.0 Control Creation Edition



This article was previously published under Q172329

SYMPTOMS

You get run-time error 459 "Object or Class does not support the set of elements" when assigning an object of class "A" to an object variable of class "B" in order to use an alternate interface.

CAUSE

The base class you are implementing in your object defines an Event object and your object variables are declared using the WithEvents keyword. The Visual Basic versions listed above do not support events in inherited interfaces.

RESOLUTION

This is a limitation of the versions of Visual Basic listed above.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a form with a command button and two class modules (Base_Class and Derived_Class).
  2. Add the following code to the Base_Class class module:

          Event Alert(ByVal Msg As String)
    
          Public Sub PostAlert(ByVal Msg As String)
          End Sub
    
                            
  3. Add the following code to the Derived_Class class module:

          Implements Base_Class
    
          Event Alert(ByVal Msg As String)
    
          Private Sub Base_Class_PostAlert(ByVal Msg As String)
            RaiseEvent Alert(Msg)
          End Sub
    
                            
  4. In the form module, add the following code:

          Dim WithEvents objDC As Derived_Class, WithEvents objBC As Base_Class
    
          Sub Command1_Click()
            Set objDC = New Derived_Class
            Set objBC = objDC              ' error 459 occurs on this line
            objBC.PostAlert "Some Message"
            Set objBC = Nothing
            Set objDC = Nothing
          End Sub
    
          Sub DC_Alert(ByVal Msg As String)
            MsgBox Msg
          End Sub
    
                            

Notes:

  • If you remove the WithEvents keyword from the objBC variable declaration, the code will run.
  • If you remove the Event declaration in the Base_Class class module, the compiler will also require you to remove the WithEvents keyword on the variable declaration, and the code will run.
  • If you remove the Event declaration in the Derived_Class class module, you will get a compile error.
  • You cannot implement the Derived_Class event handler as a Private event or derived event as you can with methods. For example, either of the following in the Derived_Class class module will result in a compile error:

          Private Event Base_Class_Alert(ByVal Msg As String)
          Event Base_Class_Alert(ByVal Msg As String)
    
                            



Additional query words: WithEvents Implements kbdse kbDSupport kbVBp kbVBp500 kbVBp600

Keywords: kbprb KB172329