Inherited class constructors

Considering the following class:

        class EngineObject
        {
            #region EngineObject Properties
            // ...
            #endregion

            #region EngineObject Constructors
       
                public EngineObject()
                {
                    this.Name = System.Guid.NewGuid().ToString();
                }

                public EngineObject(string InitName)
                {
                    this.Name = InitName;
                }

            #endregion

            #region EngineObject Public Functions
            // ...
            #endregion
        }

the theory is that, if a string argument is given when an object is created, that string argument is stored in the Name property.  if no argument is given, a unique string is generated (to prevent failure of subsequent methods).  If I now derive a class from that:

        class Class1 : EngineObject
        {
        }

and create the following from my main routine:

        EngineObject obj1 = new EngineObject()
        EngineObject obj2 = new EngineObject("test2")
        Class1 obj3 = new Class1()
        Class1 obj4 = new Class1("test4")

        Console.WriteLine("{0}", obj1.Name)
        Console.WriteLine("{0}", obj2.Name)
        Console.WriteLine("{0}", obj3.Name)
        Console.WriteLine("{0}", obj4.Name)

I am told that no overload is present in Class1 that accepts a string.  I have tried using

        public extern Class1(string InString);

but that did not work.  If i comment out the declaration of obj4 and the relevant Console output line, I can see the following:

        12345-67890-abcdef (etc)
        test2
        fedcba-09876-54321 (etc)

So it seems that Class1 inherits EngineObject() but not EngineObject(string InName).  How do I specify that I want to use the code in my base constructors as the inherited ones?

J.
LVL 16
jimbobmcgeeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

eventprostrategiesCommented:
I've had the same problem before.  Overloaded constructors simple don't seem to be inherited in .NET classes for whatever reason ...

I always (as annoying as it is) just rewrite whatever overloaded constructors i want to use in the inherited class.

Found this ...

http://www.dotnet247.com/247reference/msgs/3/17112.aspx

... but it looks like it still involves rewriting the base class's constructors ....
cupawntaeCommented:
You need a:

public Class1(string InString):base(InString) {
}

CupawnTae
eventprostrategiesCommented:
EDIT:

figured it out.  (the link *does* solve the problem)  it does involve rewriting all of the overloaded constructors, but you pass the params to the base class's constructor.

public class Parent {
   
    public Parent(int x) {
        Console.WriteLine("Parent: {0}", x);
    }

}    // Parent

public class Child : Parent {

    public Child(int x) : base(x) {
        Console.WriteLine("Child: {0}", x);
    }
}    // Child

[ for vb folks it'd be like ...
sub new(a as string, b as string)
   mybase.new(a, b)
end sub ]

good question!  now i know howto get to overloaded base class constructors  :D

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cupawntaeCommented:
You don't have to rewrite them, you just have to declare the ones you want with the :base(param) bit. You can add code if you like (which will execute *after* the base constructor) but if you leave the constructor empty it will do what you want.
jimbobmcgeeAuthor Commented:
Thanks.  I'll be posting more of these, soon...!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.