1

I've looked and didn't quite find similar to what I'm dealing with. Hopefully a solution, or offer for better implementation. I have a class that may be passed with an optional parameter that is preserved to the instance created. I then have a derived class with similar optional parameter, but in its non-parameter constructor does other preparation stuff regardless of the optional parameter. What would be the correct way to have it call the base-class parameter constructor, yet still do the non-parameter constructor of the derived.

public class MyBaseClass
{
   protected object preserveParm;
   protected int someValue;

   public MyBaseClass()
   {
      someValue = 1;
   }

   public MyBaseClass( object SomeParm ) : this()
   {
      preserveParm = SomeParm;
   }
}

public class DerivedClass : MyBaseClass
{
   private int customSecondaryProp;
   private DateTime when;

   public DerivedClass()
   {
      customSecondaryProp = 10;
      someValue = 5;
   }

   public DerivedClass( object SomeParm ) : base( SomeParm )
   {
      when = DateTime.Now;
   }
}

So, if I do a

DerivedClass test = new DerivedClass( "testing" );

I need it to hit the base class to preserve the parameter, yet ALSO hit the derived class's non-parameter to set the sample bogus values.

As it stands now, the Derived parameter method is hit, which then hits the base class's parameter constructor which calls the "this()" of the base class, and returns up the chain to the DerivedClass parameter constructor, finishes itself, but never hits the DerivedClass constructor. Is there a way to force both the baseclass parameterless AND the DerivedClass parameterless constructor?

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
DRapp
  • 47,638
  • 12
  • 72
  • 142
  • 3
    I'd normally have the non-parameterized constructor call the parameterized constructor with a suitable value, rather than the way around you're doing it. Then all of the logic ends up in the most-parameterized constructor (of the class) and it can invoke the most appropriate base class constructor (which can then, if required, again fill in omitted parameters to calls its most parameterized form) – Damien_The_Unbeliever Jan 30 '14 at 15:13
  • @Damien_The_Unbeliever, never thought of the reverse because the actual incoming object is more complex than just a simple "object", but will look into it. Thanks. – DRapp Jan 30 '14 at 15:15
  • @nawfal, for questions where a solution was already chosen, don't bother trying to backfil comments to duplicates... just a waste in my opinion. – DRapp Feb 08 '14 at 15:46

3 Answers3

3

It seems you want something like:

public DerivedClass( object SomeParm ) : base( SomeParm ), this()

But this is impossible in C#: see this answer for details I suggest you to implement the Derived constructor initializations in a method and call it manually in both constructors:

public class DerivedClass : MyBaseClass
{
    private int customSecondaryProp;
    private DateTime when;

    private void InitializeDerivedClass()
    {
        customSecondaryProp = 10;
        someValue = 5;
    }

    public DerivedClass()
    {
        InitializeDerivedClass();
    }

    public DerivedClass(object SomeParm): base(SomeParm)
    {
        InitializeDerivedClass();
        when = DateTime.Now;
    }
}

If possible, you can also use the following syntax for initialization:

    private int customSecondaryProp = 10;
Community
  • 1
  • 1
AlexH
  • 2,650
  • 1
  • 27
  • 35
0

Why not just use an optional parameter here to force the type to have only a single constructor.

public DerivedClass(object SomeParam = null) : base(SomeParam) {
  customSecondaryProp = 10;
  someValue = 5;
  if (SomeParam != null) { 
    when = DateTime.Now;
  }
}

public MyBaseClass(object SomeParam) {
  someValue = 1;
  preserveParam = SomeParam;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Because regardless of the parameter I want to set other things, the class does not REQUIRE the parameter. – DRapp Jan 30 '14 at 15:13
  • @DRapp that's what an optional parameter gives you. It can be called with `new DerivedClass()` or `new DerivedClass(someObj)` – JaredPar Jan 30 '14 at 15:14
0
public DerivedClass():base(null)

You can send Null to the parameter constructor

Amir Katz
  • 1,027
  • 1
  • 10
  • 24