473,503 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance Question

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
jk*****@vt.edu
Nov 21 '05 #1
3 852

"J. Shane Kunkle" <sh***@caudillweb.com> wrote in message
news:en**************@TK2MSFTNGP12.phx.gbl...
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
Nov 21 '05 #2
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

"J. Shane Kunkle" <sh***@caudillweb.com> wrote in message
news:en**************@TK2MSFTNGP12.phx.gbl...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
jk*****@vt.edu

Nov 21 '05 #3
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

"J. Shane Kunkle" wrote:
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
jk*****@vt.edu

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3720
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the...
2
2172
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can...
4
12799
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error...
8
7815
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
22
23315
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
6309
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
6
2083
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
5
2448
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
3
1807
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I...
8
328
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.