views:

768

answers:

2

I have a COM-visible method which looks something like the following:

Public Sub SomeMethod(someControl as Object)
On Error Goto ErrHandler    

Dim someSpecificControl as SpecificControl

   MsgBox TypeOf someControl is Control
   MsgBox TypeOf someControl is SpecificControl

   On Error Resume Next
   Set someSpecificControl = someControl
   On Error Goto ErrHandler
   if someSpecificControl is Nothing then
      Exit Sub
   end if

   ' do stuff to the control

End Sub

Other components would call this method (i.e. via COM) and pass in a control of type SpecificControl.

My problem is that when run via the debugger, the parameterized control doesn't appear to be of the right type i.e. it exits the sub-routine after the 'cast' fails when I would have expected it not to.

Using TypeOf I have verified that the parameterized object is of type Control (as above) but I cannot work out why it was passed in - apparently - incorrectly. It seems to be behaving correctly when run outside the debugger - but I can't be sure (hence this question).

Can anyone shed any light on this? Could the control have been - somehow - corrupted in the boxing-unboxing process? Is there a better way of doing this?

Edit: I used TypeName as suggested by Kris Erickson and got some interesting results:

MsgBox TypeName(someControl) 
MsgBox "someControl is of type SpecificControl: " & TypeOf someControl is SpecificControl
MsgBox "someControl is of type UserControl: " & TypeOf someControl is UserControl
MsgBox "someControl is of type Control: " & TypeOf someControl is Control

I get:

SpecificControl
someControl is of type SpecificControl: False
someControl is of type UserControl: False
someControl is of type Control: True

I guess the only way I have around this is to avoid passing in a UserControl as a parameter.

+1  A: 

I don't know why this happens, but I do know that UserControl's are semi magic in VB6. If you pass a UserControl into a function by its actual type, it loses all of its UserControl base class (I know, VB6 doesn't have inheritance but when creating a UserControl you expect certain features).

So if you

Private Sub AdjustControlProperties(oControl as MyUserControl)
...
End Sub

Once the control leaves your your subroutine, it will behave as a Control, not as a UserControl (you will have no access to UserControl properties anymore, and attempts to access them will cause an error). Very strange bug in VB6, and one that caused much pulling of hair to work it out.

Private Sub AdjustControlProperties(oControl as Object)
...
End Sub

And everything is fine. My guess is that you are correct, and UserControls are boxed, unboxed as Control's not UserControls. The only solution to type checking, is to use

TypeName()

to know what type it is, as that does not get corrupted.

Kris Erickson
Probably best then that I try to avoid passing in the control as a parameter in the first place. Thanks!
jpoh
A: 
wqw