Inheritance Question

  • Thread starter J. Shane Kunkle
  • Start date
J

J. Shane Kunkle

I took this example directly out of the book "Programming MS Visual
Basic.net" - they say this code works:

Class Person
Public FirstName As String
Public LastName As String
End Class

Class Employee
Inherits Person

Public Salary As Integer
End Class

Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim p As New Person
p.FirstName = "Ralph"
p.LastName = "X"

Dim emp As Employee = DirectCast(p, Employee) ' Exception When This
Line Executes
emp.Salary = 1000
End Sub

When i try to cast the base to the inherited class i get a
"System.InvalidCastException: Specified cast is not valid."

The book says specifically that this code should execute fine so i think i
am missing something small - any ideas?

Thanks in advance,
J. Shane Kunkle
(e-mail address removed)
 
D

David Browne

J. Shane Kunkle said:
I took this example directly out of the book "Programming MS Visual
Basic.net" - they say this code works:

Class Person
Public FirstName As String
Public LastName As String
End Class

Class Employee
Inherits Person

Public Salary As Integer
End Class

Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim p As New Person
p.FirstName = "Ralph"
p.LastName = "X"

Dim emp As Employee = DirectCast(p, Employee) ' Exception When
This Line Executes
emp.Salary = 1000
End Sub

When i try to cast the base to the inherited class i get a
"System.InvalidCastException: Specified cast is not valid."

The book says specifically that this code should execute fine so i think i
am missing something small - any ideas?

You are not missing anything. This code should not execute.

If you changed the line
DIm p as New Person
to
Dim p as Person = new Employee

it will work.

The line
Dim p as New Person
creates a non-Employee Person. You cannot downcast to Employee unless the
object really _is_ an Employee. You can always treat an Employee as a
Person, but not vice versa.

David
 
J

Jay B. Harlow [MVP - Outlook]

J. Shane Kunkle,
Either you typed the sample incorrectly or the book is flat out wrong.

I don't have the book, so I don't know what the book stated.

DirectCast requires the actual object to either Inherit from or Implement
the type that is listed.

Your p variable is instantiated as a Person object, Seeing as Employee
inherits from Person, it cannot be an Employee object, hence the
InvalidCastException.

To get your sample to work, try:
Dim p As Person = New Employee
p.FirstName = "Ralph"
p.LastName = "X"

Dim emp As Employee = DirectCast(p, Employee)

Notice that we are initializing the p variable with an Employee object,
seeing as p now contains an Employee object, the DirectCast will work.

Hope this helps
Jay
 
G

Guest

DirectCast comes in handy when you are looping through a group of objects
that all inherit from a common type:

dim e as Person = New employee
....
dim c as Person = New customer
....

....Put employees and customers in a Collection

Dim p as Person
For Each p in PersonGroupCollection
Select Case p.gettype.name
case "Person"
...do this
case "Customer"
...do that
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top