I decided to run Visual Studio and do some testing because my comments on the other answers would probably not make much sense. Assuming you have the following classes:
Public Class Vehicle Public Property Model As String Public Property Make As String End Class Public Class Truck : Inherits Vehicle Public Property IsFlatbed As Boolean End Class Public Class Car : Inherits Vehicle Public Property LeatherSeats As Boolean End Class
Another class may also have the following methods:
Private Sub WhichVehicle() Select Case cmbVehicle.SelectedItem Case Truck v = New Truck Case Car v = New Car End Select SetFlat(v) End Sub Private Sub SetFlat(ByVal myVehicle As Vehicle) If TypeOf myVehicle Is Car Then Debug.WriteLine("This is a car") Dim c As Car = DirectCast(myVehicle, Car) c.LeatherSeats = False ElseIf TypeOf myVehicle is Truck Then Debug.WriteLine("This is a truck") Dim t As Truck = DirectCast(myVehicle, Truck) t.IsFlatbed = True End If End Sub
Thus, this allows you to go around the vehicle at your discretion, because you will not know until you see what type of vehicle you have in mind (car or truck). The SetFlat method at run time can determine which specific subclass of the vehicle it has passed and act accordingly. You just need to make sure that you apply the common vehicle object (v) to the new object of a more specific subclass after you have determined what type of subclass it has (either c for Car or t for Truck), otherwise the code will not compile because you are trying to invoke a method that does not exist on a universal type of vehicle, but only on certain subtypes of vehicles.
The biggest drawback that I see in this approach is that it can get tired if you have a lot of code. This is due to the fact that each method that you want to call will have to be checked to find out what type of a specific object of the vehicle it has passed, and to start a different set of routines depending on that particular type.
source share