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.