If I use "TypeOf" on an object comparing it to a type that it can never be, I get the error "Expression of type [ ]can never be of type [ ]." That's useful. But if I use "Is" to compare one object against another which is of a type that the first can never be, I don't even get a warning. That's led to a few mistakes, and it would be really helpful if I did get some sort of warning. Is there any alternative to creating a function like "StrongIs" below if I want to get some notice of this sort of mistake?
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x As Button = Me.Button1
If TypeOf x Is Form Then Beep() 'gives an error
If x Is Me Then Beep() 'why can't this give an error?
If StrongIs(x, Me) Then Beep() 'Is there any alternative to this?
End Sub
Friend Function StrongIs(ByVal a As Object, ByVal b As Object) As Boolean
If a.GetType Is b.GetType Then
Return a Is b
Else
Err.Raise(1)
End If
End Function
End Class