MultiLevel Inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?YW1pcg==?=

    MultiLevel Inheritance

    Here's a scenario:

    MainObject
    {
    ObjectB myB;
    myB = new ObjectB(P, true);
    }

    ObjectA
    {
    variable v1;
    variable v2;
    ObjectA(param P)
    {
    v1 = P.something;
    v2 = P.somethingelse ;
    }
    }

    ObjectB: ObjectA
    {
    ObjectC myC;
    ObjectB(param P, bool check) : base(P)
    {
    if(check)
    myC = new ObjectC(P);
    }
    }

    ObjectC : ObjectB
    {
    constructorObje ctC(param P): (base P)
    {
    }
    }
    =============== ===

    I am trying to do the above but I am getting errors. I want to create Object
    C only if the boolean is true and not create it otherwise. How can I go about
    doing this?

    Thanks in advance for your help.
  • Jon Skeet [C# MVP]

    #2
    Re: MultiLevel Inheritance

    amir <amir@discussio ns.microsoft.co mwrote:
    Here's a scenario:
    You've written a lot of pseudocode without explaining what you want it
    to do. Could you try to write what you want in as close to real C# as
    possible? Also explain what you want to achieve.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Peter Duniho

      #3
      Re: MultiLevel Inheritance

      On Thu, 02 Oct 2008 15:08:02 -0700, amir <amir@discussio ns.microsoft.co m>
      wrote:
      [...]
      I am trying to do the above but I am getting errors. I want to create
      Object
      C only if the boolean is true and not create it otherwise. How can I go
      about
      doing this?
      Next, please remember to be _specific_ about your question. For example,
      you write "but I am getting errors". What errors? Where? Under what
      circumstances? When you compile? When you run the application?

      It's also helpful if you actually post code that compiles. As opposed to
      code that has a method named "constructorObj ectC" that illegally tries to
      call a base constructor.

      That said, my mind-reading hat seems to be working okay today, and I can
      explain to you that at a minimum you have an illegal call to the base
      constructor in ObjectC, because ObjectB only has a two-argument
      constructor and you're trying to call a single-argument constructor.

      Furthermore, if you posted the exact code that you compiled then obviously
      you will want to fix your constructor so that it's named "ObjectC" instead
      of "constructorObj ectC". In that case, you also will want to add an
      accessibility modifier to the constructors so that they aren't private.

      Which of these mistakes actually applies to the foremost problem you're
      running into I can't say because you haven't shared enough information
      about the errors. But hopefully the code you posted is close enough to
      the code you're actually trying to compile that the above is in some way
      helpful.

      Pete

      Comment

      • DaveL

        #4
        Re: MultiLevel Inheritance

        hope the below helps out

        DaveL


        namespace console1
        {
        public static class console1
        {
        public static void main()
        {
        AbstractTest o1 = new Inherit1();
        AbstractTest o2 = new Inherit2();
        AbstractTest o3 = new Inherit3();
        }
        }
        // in the namespace

        public abstract class AbstractTest
        {
        public int Field1;
        public int Field2;
        public AbstractTest()
        {
        }
        public virtual String HelloWorld()
        {
        Console.WriteLi ne("In Abstract Class");
        return "";
        }
        }
        public class Inherit1 : AbstractTest
        {
        public Inherit1()
        {
        }
        public override string HelloWorld()
        {
        Console.WriteLi ne("Do Somthing Different");
        return "";
        }
        }
        public class Inherit2 : Inherit1
        {
        public Inherit2()
        {
        }
        }
        public class Inherit3 : AbstractTest
        {
        public Inherit3()
        {
        }
        }


        }
        "amir" <amir@discussio ns.microsoft.co mwrote in message
        news:EAA9594D-E299-4A28-8844-E5C34EC8AD05@mi crosoft.com...
        Here's a scenario:
        >
        MainObject
        {
        ObjectB myB;
        myB = new ObjectB(P, true);
        }
        >
        ObjectA
        {
        variable v1;
        variable v2;
        ObjectA(param P)
        {
        v1 = P.something;
        v2 = P.somethingelse ;
        }
        }
        >
        ObjectB: ObjectA
        {
        ObjectC myC;
        ObjectB(param P, bool check) : base(P)
        {
        if(check)
        myC = new ObjectC(P);
        }
        }
        >
        ObjectC : ObjectB
        {
        constructorObje ctC(param P): (base P)
        {
        }
        }
        =============== ===
        >
        I am trying to do the above but I am getting errors. I want to create
        Object
        C only if the boolean is true and not create it otherwise. How can I go
        about
        doing this?
        >
        Thanks in advance for your help.

        Comment

        Working...