Posts

Primary Constructor in C# 6.0

  In my previous post I explain about Auto Property Initializer in C# 6.0. Now lets see another new feature in C# 6.0 which is Primary constructor. First we will see the following code. public class Country {      public string Name { get ; private set ; }        public Country( string name)          {          Name = name;          }   } here we have a class which has a property with a private setter and assigning the value for the name property inside the constructor. Now in C# 6.0 you can write the above code in a more simplified manner as follows. public class Country ( string name)      {          public string Name { get ;}=name;          } This is what is called primary constructor in C# 6.0 . Notice the parameters added just right of the class definition. This tells the compiler to add a constructor to the Country class with one parameters: name. So we don’t need to create separate constructor with par...

Auto Property Initializer

  Anyone who have worked with properties in a class must have experienced that it was little annoying that we have to have private fields and assigned if we want to have default values for properties. See the following example which we used to do when we have collection property. public class Country      {          private List < string > _regions = new List < string >();          public List < string > Regions { get { return _regions} set { value = _regions; } }        } but this is no more needed. We can simply write it as below. public class Country      {          public List < string > Regions { get ; set ; }= new List < string >();        } Also this is useful when you want to assign default values for read only fields. Before C# 6.0 public class Country    {        public string Name { get ; }        public Country()        {            this .Name = "Sri Lanka" ; ...

Injecting a service into a view in MVC 6

Image
  Asp.net MVC 6 supports injecting service to view using a class. Example :- When country combo box value get changed , region com Only restriction is that class should be Public Non-nested Non-Abstract 1. Create a Asp.Net Project in Visual Studio 2015     2. Create a folder name services and add a service class. using System; using System.Threading.Tasks;   namespace ViewInjection.Services {      public class EmployeeService      {          public async Task < int > GetEmployeesOnLeaveCount()          {              return await Task .FromResult(5);          }            public async Task < int > GetEmployeesOnSiteCount()          {              return await Task .FromResult(                  10);          }            public async Task < int > GetEmployeesOnWorkCount()          {              return await Task .FromResult(97);          }      } }   2. Create a View to ...

Visual Studio 2014–New features

Image
  Visual studio 2014 CTP is now available for download. You can download it from here . With the new release there are some new language & IDE features introduced. I will discuss some of IDE improvements in this post. IDE Improvements 1. Colored quick info when you hover over on something in your code now the quick info window has information with colors. also if you collapse your code & hover over to see the code inside so you can see the code with all colored formatting in the tool tip. 2. Hint with un-used references When we create a class there are few references which are automatically added. Also there are some cases we add some piece of code which need some other references & sometime back we remove that code but we don’t remove the reference. Now Visual studio grey out those references & give option to remove those. 3. Hint with redundant qualifiers In your code if you have already added the reference in your code & have used the namespace ...

C# Design Patterns

  Please find the presentation and the sample code I presented on Sri Lanka .Net forum on August 2014 Meet up. August 2014

Cannot read property 'fnSetData' of undefined

  I have a Jquery Datatable in one of my MVC view. When I tried to load the Datatable I got the following error. Cannot read property 'fnSetData' of undefined Problem in my case was my table header and the table body content was not matching. ex: < table cellpadding ="0" cellspacing ="0" border ="0" class ="display" id ="TBL_WorkAreaManagementSearchResults" style ="width: 100%" > < thead > < tr > < th ></ th > < th > WorkArea </ th > < th > Region </ th > </ tr > </ thead > < tbody > @foreach (var workArea in Model) { < tr >...

Editable List box in HTML

  I have recently came across a requirement where  a list of objects need to be added and edited. That object has only two fields which is name & id. So there is no point of using a editable data table as well. Following is the solution I came across. Create a List box Add a text box and a button which will add new items to the list box. On click on list box item load a pop up which allows user to edit the items.   Code for generating Listbox at the View < div >                         < select size ="10" id ="workAreas" multiple >                          @ foreach ( var item in Model.WorkAreas)                          {                                             < option id ="option_ @ item.Id " value =" @ item.Id " onclick ="new RegionManagement().OnWorkAreaEdit( @ item.Id );" class ="TextBoxLarge">                                  @ item.Name                           ...