oop - C# Object Oriented Programming Declaring Properties -


i have class , in each method declaring following lines repeatedly:

var viewspath = path.getfullpath(hostingenvironment.mappath(@"~/views/emails")); var engines = new viewenginecollection(); engines.add(new filesystemrazorviewengine(viewspath)); 

how , declare them available each method i'm not having write same line repeatedly inside each method?

public class emailservice   {     public emailservice()     {      }      public void notifynewcomment(int id)     {         var viewspath = path.getfullpath(hostingenvironment.mappath(@"~/views/emails"));         var engines = new viewenginecollection();         engines.add(new filesystemrazorviewengine(viewspath));          var email = new notificationemail         {             = "yourmail@example.com",             comment = comment.text         };          email.send();      }       public void notifyupdatedcomment(int id)     {         var viewspath = path.getfullpath(hostingenvironment.mappath(@"~/views/emails"));         var engines = new viewenginecollection();         engines.add(new filesystemrazorviewengine(viewspath));          var email = new notificationemail         {             = "yourmail@example.com",             comment = comment.text         };          email.send();      }    } 

you make them class-level members:

public class emailservice  {     private string viewspath;     private viewenginecollection engines;      public emailservice()     {         viewspath = path.getfullpath(hostingenvironment.mappath(@"~/views/emails"));         engines = new viewenginecollection();         engines.add(new filesystemrazorviewengine(viewspath));     }      public void notifynewcomment(int id)     {         var email = new notificationemail         {             = "yourmail@example.com",             comment = comment.text         };          email.send();     }      // etc. } 

this populate variables once when create new emailservice:

new emailservice() 

then method executed on instance use values created @ time.


Comments