Results 1 to 5 of 5

Thread: Casting Subclasses, Base classes

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    2

    Casting Subclasses, Base classes

    I am a VB6 programmer learning VB.Net and am having trouble with the following concept.
    I have two classes
    Base class: Person
    Subclass: Employee

    Dim obj As Person = New Employee()

    In simple terms, what is happening in memory (stack, heap)

    When I try to do this:
    Dim obj As Employee = New Person()

    I get a cast error at runtime.
    Why is that?
    Is it illegal to cast from a subclass to a base class?

  2. #2

    Re: Casting Subclasses, Base classes

    Do you have Option Strict turned On?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Casting Subclasses, Base classes

    Consider these two classes:
    vb.net Code:
    1. Public Class Person
    2.  
    3.     Private _name As String
    4.  
    5.     Public Property Name() As String
    6.         Get
    7.             Return _name
    8.         End Get
    9.         Set(ByVal value As String)
    10.             _name = value
    11.         End Set
    12.     End Property
    13.  
    14. End Class
    15.  
    16.  
    17. Public Class Employee
    18.     Inherits Person
    19.  
    20.     Private _payrollNumber As Integer
    21.  
    22.     Public Property PayrollNumber() As Integer
    23.         Get
    24.             Return _payrollNumber
    25.         End Get
    26.         Set(ByVal value As Integer)
    27.             _payrollNumber = value
    28.         End Set
    29.     End Property
    30.  
    31. End Class
    Now look at this code:
    vb.net Code:
    1. Dim p As Person = New Employee
    2.  
    3. p.Name = "John Citizen"
    That code makes sense, right? You create a new Employee object and Employee inherits Person so you know that that object is a Person so you can assign it to a Person variable and access the Name property.

    Now look at this code:
    vb.net Code:
    1. Dim e As Employee = New Person
    2.  
    3. e.PayrollNumber = 1
    This code doesn't make sense, right? You're creating a Person object, which only has a Name property, and trying to assign it to a variable of type Employee. If you have an Employee variable you would expect to be able to access its PayrollNumber property, right? This object doesn't have a PayrollNumber property though, because it's NOT an Employee, therefore it cannot be assigned to an Employee variable.

    Think about it. This is not a programming concept specifically. OOP is based on real-world objects and they behave exactly the same way. Every Cat is an Animal, but not every Animal is a Cat. Wherever you are allowed to take an Animal, you are allowed to take a Cat because a Cat is an Animal. Take a Cat to a vet and they'll treat it, right? The converse is not true though. Places that expect a Cat will not accept just any Animal. Try entering an Elephant into a Cat show and you'll see what I mean.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    2

    Re: Casting Subclasses, Base classes

    That makes a lot of sense when you put it in real world terms.
    I hope the rest of the language turns out to be that logical.

    Thank You so much!!!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Casting Subclasses, Base classes

    Quote Originally Posted by rericac View Post
    I hope the rest of the language turns out to be that logical.
    It pretty much does, although it's not always immediately obvious what the real-world analogue is. Interfaces is a great example. They can confuse at first but when you understand how they relate to real-world objects their purpose becomes much clearer. Maybe we'll see you back when you get up to learning about them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width