6

I am trying to figure out how to invoke a base class constructor when I call the derived class constructor.

I have a class called "AdditionalAttachment" which is inherited from System.Net.Mail.Attachment.I have added 2 more properties to my new class so that i can have all the properties of existing Attachment class with my new properties

public class AdditionalAttachment: Attachment
{
   [DataMember]
   public string AttachmentURL
   {
       set;
       get;
   }
   [DataMember]
   public string DisplayName
   {
       set;
       get;
   }
}

Earlier i used to create constructor like

//objMs is a MemoryStream object

Attachment objAttachment = new Attachment(objMs, "somename.pdf")

I am wondering how can I create the same kind of constructor to my class which will do the same thing as of the above constructor of the base class

halfer
  • 19,824
  • 17
  • 99
  • 186
Shyju
  • 214,206
  • 104
  • 411
  • 497

4 Answers4

13

This will pass your parameters into the base class's constructor:

public AdditionalAttachment(MemoryStream objMs, string displayName) : base(objMs, displayName)
{
   // and you can do anything you want additionally 
   // here (the base class's constructor will have 
   // already done its work by the time you get here)
}
Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
7

You can write a constructor that calls the class base constructor:

public AdditionalAttachment(MemoryStream objMs, string filename)
    : base(objMs, filename)
{
}
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
7

Use this function:

public AdditionalAttachment(MemoryStream ms, string name, etc...)
       : base(ms, name) 
{
}
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50
3
public class AdditionalAttachment: Attachment
{
   public AdditionalAttachment(param1, param2) : base(param1, param2){}
   [DataMember]
   public string AttachmentURL
   {
       set;
       get;
   }
   [DataMember]
   public string DisplayName
   {
       set;
       get;
   }
}
Itay Karo
  • 17,924
  • 4
  • 40
  • 58