You can totally do it like this:
Sub DoSomething(ByVal arg As Object)
If TypeOf arg Is TypeX Then
DoSomethingX(DirectCast(arg, TypeX)
ElseIf TypeOf arg Is TypeY Then
DoSomethingY(DirectCast(arg, TypeY))
End If
End Sub
But: at least first ask yourself if you're trying to do what should really be done with inheritance and polymorphism.
That's a big word for basically this:
Public MustInherit Class BaseType
Public MustOverride Sub DoSomething()
End Class
Public Class TypeX
Inherits BaseType
Public Overrides Sub DoSomething()
' Whatever TypeX does. '
End Sub
End Class
Public Class TypeY
Inherits BaseType
Public Overrides Sub DoSomething()
' Whatever TypeY does. '
End Sub
End Class
It looks like a lot of typing, but then down the road any time you have an object of type BaseType
, instead of a bunch of If
/ElseIf
checks, you just do:
arg.DoSomething()
Might not be the solution in your case. Just wanted to point out that this exists, and might be a better solution to your problem (hard to say without details).