Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

I am Jalpesh Vadgama a Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 6 years of experience in .NET Technology.
Browse by Tags · View All
#ASP.NET 88
#DOTNET 87
BRH 79
ASP.NET 72
#.NET 52
C#.NET 48
DOTNET 47
ASP.NET 4.0 31
ASP.NET MVC 29
VisualStudio 27

Archive · View All
December 2010 16
July 2011 13
April 2011 13
April 2012 12
January 2011 12
June 2011 11
May 2011 11
May 2012 8
February 2013 7
January 2013 7

Code refactoring with Visual Studio 2010 Part-4

Jun 29 2011 5:01PM by Jalpesh   

I have been writing few post with code refactoring features in Visual Studio 2010. This post also will be part of series and this post will be last of the series. In this post I am going explain two features 1) Encapsulate Field and 2) Extract Interface. Let’s explore both features in details.

Encapsulate Field:

This is a nice code refactoring feature provides by Visual Studio 2010. With help of this feature we can create properties from the existing private field of the class. Let’s take a simple example of Customer Class. In that I there are two private field called firstName and lastName. Below is the code for the class.

public class Customer
{
    private string firstName;
    private string lastName;
    public string Address { get; set; }
    public string City { get; set; }
    
}

Now lets encapsulate first field firstName with Encapsulate feature. So first select that field and goto refactor menu in Visual Studio 2010 and click on Encapsulate Field. Once you click that a dialog box will appear like following.

EncapsulateField

Now once you click OK a preview dialog box will open as we have selected preview reference changes. I think its a good options to check that option to preview code that is being changed by IDE itself. Dialog will look like following.

PreviewEncapsulateField

Once you click apply it create a new property called FirstName. Same way I have done for the lastName and now my customer class code look like following.

public class Customer
{
    private string firstName;

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }
    private string lastName;

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }
    public string Address { get; set; }
    public string City { get; set; }
    
}

So you can see that its very easy to create properties with existing fields and you don’t have to change anything there in code it will change all the stuff itself.

Extract Interface:

When you are writing software prototype and You don’t know the future implementation of that then its a good practice to use interface there. I am going to explain here that How we can extract interface from the existing code without writing a single line of code with the help of code refactoring feature of Visual Studio 2010. For that I have create a Simple Repository class called CustomerRepository with three methods like following.

public class CustomerRespository
{
   public void Add()
   {
       // Some code to add customer
   }
    
   public void Update()
   {
       //some code to update customer
   }

   public void Delete()
   {
       //some code delete customer
   }
}

In above class there are three method Add,Update and Delete where we are going to implement some code for each one. Now I want to create a interface which I can use for my other entities in project. So let’s create a interface from the above class with the help of Visual Studio 2010. So first select class and goto refactor menu and click Extract Interface. It will open up dialog box like following.

ExtactInterface

Here I have selected all the method for interface and Once I click OK then it will create a new file called ICustomerRespository where it has created a interface. Just like following.

SolutionExplorer

Here is a code for that interface.

using System;
namespace CodeRefractoring
{
    interface ICustomerRespository
    {
        void Add();
        void Delete();
        void Update();
    }
}

Now let's see the code for the our class. It will also changed like following to implement the interface.

public class CustomerRespository : ICustomerRespository
{
   public void Add()
   {
       // Some code to add customer
   }
    
   public void Update()
   {
       //some code to update customer
   }

   public void Delete()
   {
       //some code delete customer
   }
}
Isn't that great we have created a interface and implemented it without writing a single line of code. Hope you liked it. Stay tuned for more.. Till that Happy Programming.
Shout it

Tags: C#.NET, BRH, #.NET, VisualStudio, VisualStudio2010, Code Refactor, CodeRefactor,


Jalpesh
15 · 11% · 3548
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]