-1

I was just wondering, I am currently writing a data model in C# for a blog site, and I'm using derived classes. But the annoying thing is, I have to initialize the base class' fields in the derived class' constructor, like below:

public class baseClass
{
  public dateTime dateCreated { get; protected set; } //only protected because that's what I need...
}

public class derivedClas : baseClass
{
  public derivedClass
  {
    this.dateCreated = DateTime.Now;
  }
}

So what I want to do is instead of manually setting the field in the derived class, I want to set it automatically in the baseClass whenever a new instance of derivedClass is created. Is this possible?

TechnicalTophat
  • 1,655
  • 1
  • 15
  • 37

4 Answers4

0

If you implement a constructor in the base class that has a matching signature (same parameter types) as your derived class, the base constructor will automatically be called.

public class BaseClass
{
    public DateTime DateCreated { get; protected set; } //only protected because that's what I need...

    public BaseClass()
    {
        DateCreated = DateTime.Now;
    }

}

public class DerivedClass : BaseClass
{
    public DerivedClass()
    {
    }
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
  • Thanks, but that's not what I want to do. I want to be able to omit the initialization from the derived class constructor, so it's automatically set to a predefined value in the baseClass constructor – TechnicalTophat Jan 10 '17 at 18:22
  • Take a look at my edit. Apparently my initial answer wasnt clear enough. – maccettura Jan 10 '17 at 18:24
  • 2
    The base constructor call is implicit if not specified, as is the empty constructor body. There's no need for any of that code in the derived class. – Servy Jan 10 '17 at 18:27
  • @maccettura Now you have a constructor that's not supposed to be there, as the question specifically indicated that this value shouldn't be set publicly, and you're also improperly copying a date (by losing the time and timezone information). – Servy Jan 10 '17 at 18:48
  • The example isn't real world. I am trying to retain as much of their code as possible. I wanted to make my answer topical so the OP would have contextual answers. Would it satisfy more users if I just add/remove code? – maccettura Jan 10 '17 at 18:51
0

Is this what you're hoping to achieve?

void Main()
{
    var i = new B();
    i.Time.Dump();
}

// Define other methods and classes here
public class A
{
    public A()
    {
        Time = DateTime.Now;
    }
    public DateTime Time {get;set;}
}

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

    }
}
Noobian
  • 35
  • 3
0

This should do the trick for you:

public class baseClass
{
  public dateTime dateCreated { get; protected set; } 
  protected baseClass() {
    dateCreated = DateTime.Now;
  }
}

public class derivedClas : baseClass
{
  public derivedClass() : base()
  {
   //Do some constructor stuff for derivedClass if needed
  }
}

this will call the constructor in the base class before the constructor in the derived class.

Or if you just want default values for your date you can do this:

public class baseClass
{
  private DateTime _date = DateTime.Now;
  public DateTime dateCreated { get {return _date;} protected set { _date = value;} } 

}

That should set the date when the derived class is instantiated.

Dan
  • 1,451
  • 1
  • 11
  • 8
0

Unfortunately, DateTime.Now is not a compile-time constant, or we could have used it here as a default value. Instead we will need to not use an auto-implemented property in the base class. That lets our field actually be a nullable type, so we can initialize it with a check on its value without a NullReferenceException.

 public class BaseClass
    {      
      private DateTime? _dc;  //notice, it's nullable
      public DateTime DateCreated 
          {
              get 
              {
                 if(_dc == null)
                    _dc = InitDate();
                 return (DateTime)_dc;
              }
              set
              { _dc = value; }

          }
       private static DateTime InitDate()
       { return DateTime.Now; }
  }

  public class DerivedClass : BaseClass
  {
      public DerivedClass
      {           
      }
  }

If you don't mind the default value being equivalent to DateTime.MinValue instead, you can use a default parameter.

    public class BaseClass
    {
        public DateTime DateCreated { get; set; } = new DateTime();

    }
CDove
  • 1,940
  • 10
  • 19