L
Larry Serflaten
I am trying to build a chart to help me track what gets called when
in the inheritance chain. I came across an error that has me puzzled.
I have a derived class that inherits from a base class, but when I declare
a variable of the derived type, I can't assign a base type to it.
I am puzzled because the derived type 'is a' base type, so why can't a
variable of the derived type be assigned a value of base type?
For brievity, here is the offending line (Strict on or off makes no
difference)
Dim parent As New Base
Dim daughter As Derived
daughter = parent ' Error : Cast not valid
Here are the actual Base and Derived classes:
Public Class Base
Public Sub AAA()
Console.WriteLine("Base:AAA")
End Sub
Public Sub BBB()
Console.WriteLine("Base:BBB")
End Sub
Public Overridable Sub CCC()
Console.WriteLine("Base:CCC")
End Sub
End Class
Public Class Derived
Inherits Base
Public Overloads Sub BBB()
Console.WriteLine("Derived:BBB")
End Sub
Public Overrides Sub CCC()
Console.WriteLine("Derived:CCC")
End Sub
Public Sub DDD()
Console.WriteLine("Derived
DD")
End Sub
End Class
I have tried commenting everything out of the derived class except
the Inherits line, and it still did not work. Am I mistaken in thinking
that it should work?
LFS
in the inheritance chain. I came across an error that has me puzzled.
I have a derived class that inherits from a base class, but when I declare
a variable of the derived type, I can't assign a base type to it.
I am puzzled because the derived type 'is a' base type, so why can't a
variable of the derived type be assigned a value of base type?
For brievity, here is the offending line (Strict on or off makes no
difference)
Dim parent As New Base
Dim daughter As Derived
daughter = parent ' Error : Cast not valid
Here are the actual Base and Derived classes:
Public Class Base
Public Sub AAA()
Console.WriteLine("Base:AAA")
End Sub
Public Sub BBB()
Console.WriteLine("Base:BBB")
End Sub
Public Overridable Sub CCC()
Console.WriteLine("Base:CCC")
End Sub
End Class
Public Class Derived
Inherits Base
Public Overloads Sub BBB()
Console.WriteLine("Derived:BBB")
End Sub
Public Overrides Sub CCC()
Console.WriteLine("Derived:CCC")
End Sub
Public Sub DDD()
Console.WriteLine("Derived

End Sub
End Class
I have tried commenting everything out of the derived class except
the Inherits line, and it still did not work. Am I mistaken in thinking
that it should work?
LFS