Link to home
Start Free TrialLog in
Avatar of jimbobmcgee
jimbobmcgeeFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of eventprostrategies
eventprostrategies

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 ....
SOLUTION
Avatar of cupawntae
cupawntae

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jimbobmcgee

ASKER

Thanks.  I'll be posting more of these, soon...!!