Tarik
Tarik

Reputation: 81711

Cannot cast an object to its parent object in Visual Basic

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

Upvotes: 2

Views: 3911

Answers (3)

Jason S
Jason S

Reputation: 1544

Your example is a bit mixed since you mention the classes Honda and Car but in the code you have classes A and B. I am presuming in your C# code you have

class Honda : Car {  }

In that case

Honda a = new Car(); // Cannot implicitly convert type 'Car' to 'Honda'

is trying to assign an object of type Car to a reference of type Honda. Assuming you have class Honda : Car { }, you are thus trying to assign an object of the parent type Car to a reference of the child type Honda. Your question says, "Cannot cast an object to its parent object", but you are in fact trying to do the opposite based on the code you've provided and the assumption.
Thus even in C# you should also get the compiler error of Cannot implicitly convert type 'Car' to 'Honda'. It should not work as you say.

This is due to the way inheritance works, which applies to VB and C# alike (and other OO languages such as Java).

Honda and B in your example are lower in the class hierarchy to Car and A. Meaning they have at least all the properties of their Base classes (aka Superclass or Parent class). As such the compiler can't implicitly convert a Base class to a more specialised class, although it can implicitly convert a more specialised class to its base class. That is why assigning an object of type Car to a variable of type Honda will cause a compiler error in the absence of an explicit type conversion. However you could assign an object of type Honda to a variable of type Car, or an object of type B to a variable of type A like this

Dim myHonda as Car = New Honda()
Dim bObject as A = New B()

or in C#

Car myHonda = new Honda();
A bObject = new B();

Upvotes: 0

Bryan B
Bryan B

Reputation: 4535

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.

Upvotes: 5

Tejs
Tejs

Reputation: 41236

You've got this wrong. You can never cast A as B.

This would be the valid statement:

 Dim instance as A = New B

An A can contain an instance of B, not the other way around.

Upvotes: 4

Related Questions