Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Inheritance: How to invoke the base class constructor when i call the derived class constructor

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

like image 929
Shyju Avatar asked Dec 14 '10 22:12

Shyju


1 Answers

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)
}
like image 193
Mark Avenius Avatar answered Sep 28 '22 13:09

Mark Avenius