In C# we can do something like this:
Honda a = new Car(); and that works but the same one doesn't work in Visual Basic.NET (I am fairly new to Visual Basic)
Dim a as Honda = new Car and it says Unable to cast object of type 'SampleApp.Car' to type 'SampleApp.Honda'.
What's wrong here?
Here is my sample code:
Module Module1
Sub Main()
Dim a As B = New A
Console.WriteLine(a.DoSOmething())
Console.ReadLine()
End Sub
End Module
Class A
Public Overridable Function DoSOmething() As String
Return "SOmething"
End Function
End Class
Class B
Inherits A
Public Overrides Function DoSOmething() As String
Return "Something else"
End Function
End Class
A Honda is more specific than a car, and may include additional features or behavior. You can cast a Honda to a Car without issue, but not a Car to a Honda.
edit: Example, a Honda may include an ActivateVTEC method, while all Cars will have a Refuel method, so if you were able to create a Honda = new Car, ActivateVTEC would be undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With