1

I want to implement default object pattern for my all classes in the inheritance tree. I am doing as shown below.

 namespace test
{
    public class Record
    {
        public int ID { get; set; }
    }

    public class StudentRecord : Record
    {
        public string StudentID { get; set; }
    }

    public class DriverRecord : Record
    {
        public string DLNumber { get; set; }
    }

    public class client
    {
        public static void Main()
        {
            StudentRecord st = StudentRecord.Default;
            DriverRecord dr = DriverRecord.Default;
        }
    }
}

I want the default property or method to initialize all the class level properties to their defaults and I don’t want to repeat the implementation for each class. I just want to write on Record (base ) class . Can you provide some suggestions on this?

Hari Narisetty
  • 176
  • 1
  • 2
  • 14
  • 1
    In what way do you want the properties initialized? They will all be initialized to the default value for each type by the CLR. Do you want some other default values, and if so, how shall the base class be made aware of default values for properties in derived classes? – Fredrik Mörk Apr 04 '11 at 11:53

4 Answers4

4

What you’re looking for is exactly what constructors are for. A constructor can call an inherited base constructor, so you need to do the base initialisation in only one place. Sometimes the basic functionality really does what you need :)

public class Record
{
    public int ID { get; set; }
    public Record()
    {
        // ... do general initialisation here ...
    }
}

public class StudentRecord : Record
{
    public string StudentID { get; set; }
    public StudentRecord()
        : base()    // This calls the inherited Record constructor,
                    // so it does all the general initialisation
    {
        // ... do initialisations specific to StudentRecord here ...
    }
}

public class client
{
    public static void Main()
    {
        // This calls the constructor for StudentRecord, which
        // in turn calls the constructor for Record.
        StudentRecord st = new StudentRecord();
    }
}
Timwi
  • 65,159
  • 33
  • 165
  • 230
1

The Record class can only set the properties which are inherited by StudentRecord and DriverRecord. If you want to set class-specific properties to their default values you have to override the method (I would make a method) and do something like this (for StudentRecord ):

public void override Initialize()
{
    base.Reset();

    this.StudentId = 0;
}

HTH

Martijn
  • 24,441
  • 60
  • 174
  • 261
  • Thanks. This approach needs the Initialize function to be overridden on all classes. I am looking for a functionality in such a way that, I don't have to modify this initialize method if something gets added in later. – Hari Narisetty Apr 04 '11 at 12:15
1

You don't have any "class level properties", i.e. static properties, in your code sample. The properties you do have (the instance properties) are already initialized to their defaults -- 0 for integers, null for references, etc.

If you want to define your own defaults -- perhaps ID should default to -1 until you save, and the strings should default to "" -- then that's exactly what constructors are for:

public class Record
{
    public Record() { ID = -1; }
    public int ID { get; set; }
}
public class StudentRecord : Record
{
    public StudentRecord() { StudentID = ""; }
    public string StudentID { get; set; }
}
// etc.

If you want something different from either of those, you'll have to explain what you're looking for.

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • Ok. What I am trying to achieve is, – Hari Narisetty Apr 04 '11 at 13:10
  • When ever the student property is accessed from the delcration class, I don't want to return null IF it is not initialized, I want to return default object. Again,this object is returned so that the accessing code will not throw exception. – Hari Narisetty Apr 04 '11 at 13:22
0

I think Null Object Pattern is what you need.

tienpham
  • 21
  • 3
  • I don't think so ... from the first paragraph of you link: _"The intent of a NULL OBJECT is to encapsulate the absence of an object by providing a substitutable alternative that offers suitable default do nothing behavior."_. Could you explain in what way this relates to the question? – Marijn Jun 03 '11 at 07:35