Results 1 to 3 of 3

Thread: [2005] Just and inheritance question.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Posts
    87

    [2005] Just and inheritance question.

    I dont realy have a problem or something but just wondering something.

    Is it possible that a class inherit the same instance of a specified class?

    Let me make a simple example:

    The inherited class:
    Code:
    Public MustInherit Class Person
    public _Name as string
    public _Age as string
    End Class
    Class Employe:
    Code:
    Public Class Employe
    Inherits Person
    public _Salary as Integer
    End Class
    Class Costumer:
    Code:
    Public Class Costumer
    Inherits Person
    public _MoneyToSpend as Integer
    End Class

    When i create Costumer it more then logic then its a person too.
    But when i, as Costumer, gonna work in the shop i olso become an Employe.

    But technicly when i make an Employe i olso make a new Person.

    Are there ways to solve this problem other then just make lists of wich person is what?

    Greets

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2005] Just and inheritance question.

    I don't think you can cast directly from one sub-object to another, but what may work would be something along the lines of:

    Code:
    dim p as Person
    
    dim cust as New Customer
    
    'Do customer stuff here
    
    p= CType(cust, Person)
    
    dim emp as Employee = CType(p, Employee)
    That casts the Customer back to the base Person object and then casts that Person object into an Employee.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] Just and inheritance question.

    Does that even make sense? That would suggest that every child class is the same thing, which isn't true. Every child can be cast as a member of it's base, but a base can't then be arbitrarily cast into any type of child....safely. Technically, it can be done as long as the children happen to be identical in certain ways.

    For instance, one child class (A) might add the variable Foo as an integer, while a second child (B) might not add the variable. This means that the lookup tables that the compiler uses to find the offsets of various components of the two children will be different. If you were to create an object of type A, then cast it to its base class, would it really make sense that it could be turned into a type B just by casting the base pointer into a pointer of type B? The virtual table for this pseudo-B would have an offset for foo, which wouldn't be a member of the class.

    Furthermore, since everything is derived from type Object, this would mean that you could turn a database connection into a form with only two steps (cast the connection to Object, then cast back to form), which is absurd.

    As for the current question, you are getting close to multiple inheritance, which is not implemented in VB.NET, and frankly, it causes more headaches than it is worth. In your case, I think even simple inheritance is not really what you want.

    If both customers and employees are objects, and they share the traits that you were putting into the people object, then at first it makes sense to have employees and customers inherit from a common base. However, if a single person can be both an employee and a customer, then the inheritance breaks down, because you can only create an object (employee, or customer, or person). The base class part of a child class is not a distinct object independent of the child, so it can't be separated out.

    I think that was poorly explained, but hopefully it is close enough that you can figure it out. However, that doesn't answer the question. There are two answers. One would be that you could abandon inheritance and have both customers and employees have a member that is an object of type Person. Have the constructor of the Customer and Employee classes take an object of type Person. Thus you could create an object of type Person, then create an object of type Customer passing in this Person object, then create an object of type Employee, also passing in the same Person object. The Customer and Employee objects will have the same Person object as members, but not as parent classes.

    The alternative is to implement interfaces. You would have only the single class Person, and implement two interfaces for the class, one called Customer, and another called Employee. This would mean that an object of type Person could behave as either a Customer or an Employee. You would simply cast the Person object into the correct interface type, and use that interface.
    My usual boring signature: Nothing

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