assign a base class to a derived class

Discussion in 'ASP .Net' started by Mitja Semolic, Dec 8, 2003.

  1. Could I get a comment how to assign a base object to a derived object?
    Thanks, Mitja Semolic

    public class B : A
    {
    public B: base()
    {}

    static void main()
    {
    // throws an exception 'System.InvalidCastException'
    B derived = (B) new A();
    }
    }
     
    Mitja Semolic, Dec 8, 2003
    #1
    1. Advertising

  2. Mitja Semolic

    Codemonkey Guest

    You cannot assign a base class instance to a derived class variable unless
    the instance is the derived type.

    A derived type is a superset of the base type. If you try to assign the only
    the base type (which is a subset of the derived type) to a superset
    variable, then you'll get an error. Suppose you had a base class called
    Person (Name, Age etc.) and a derived class called Employee (All of Person,
    Employee No etc.). If you only create a person object, no space has been
    allocated for the extra stuff needed by an Employee class, so you cannot
    assign

    For example:

    Assume MyClassB inherits from MyClassA

    The following will work:
    MyClassB myVariableB = New MyClassB();
    MyClassA myVariableA = (MyClassA) myVariableB;

    The following is won't work:
    MyClassA myVariableA = New MyClassA();
    MyClassB myVariableB = (MyClassB) myVariableA;

    The following will work because myVariableA is actually of type MyClassB:
    MyClassA myVariableA = (MyClassA) New MyClassB();
    MyClassB myVariableB = (MyClassB) myVariableA;


    Sorry for sounding more complicated than this is. Someone else can hopefully
    provide a better explanation of how inheritance works.

    For your code to work, you'd need to do something like:

    public class B : A
    {
    public B: base()
    {}

    static void main()
    {
    B derived = (B) new B();
    }
    }

    Hope this helps,

    Trev.

    "Mitja Semolic" <> wrote in message
    news:Ae0Bb.6425$...
    > Could I get a comment how to assign a base object to a derived object?
    > Thanks, Mitja Semolic
    >
    > public class B : A
    > {
    > public B: base()
    > {}
    >
    > static void main()
    > {
    > // throws an exception 'System.InvalidCastException'
    > B derived = (B) new A();
    > }
    > }
    >
    >
     
    Codemonkey, Dec 8, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    427
    Alf P. Steinbach
    May 23, 2007
  2. Replies:
    1
    Views:
    405
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    395
    Victor Bazarov
    May 23, 2007
  4. Replies:
    2
    Views:
    721
  5. junyangzou
    Replies:
    8
    Views:
    188
    junyangzou
    Aug 30, 2013
Loading...

Share This Page