Visual Studio Tip of the day – Refactoring – Extracting method

You notice that you have a chuck of code which could easily be transitioned to a new function. How tdo you do that?


Again, Visual Studio Refactoring menu comes to the rescue.


Suppose you have the following code in your function

public void Myfunc()

{

   Console.WriteLine(“a”);

   Console.WriteLine(“b”);

   Console.WriteLine(“c”);

   // Do some processing here.

   Console.WriteLine(“a”);

   Console.WriteLine(“b”);

   Console.WriteLine(“c”);


}


 


We realize that code containing Console.Writeline is replicated. Select one set of the Console.Writeline instructions and right click > Refactor > Extract Method…


Type the name of the new function you want to create containing the selected lines and Click OK.


A new method containing the selected lines is created. So your code will look like

public void Myfunc()

{

   NewMethod();

   // Do some processing here.

   Console.WriteLine(“a”);

   Console.WriteLine(“b”);

   Console.WriteLine(“c”);

}

private static void NewMethod()

{

   Console.WriteLine(“a”);

   Console.WriteLine(“b”);

   Console.WriteLine(“c”);

}


 


Keyboard shortcut: Ctrl R + Ctrl M


Cavaet: You will have to delete the second set manually as currently VS editor is not smart enough to replace all the occurances of the selected lines. Maybe in the next version we can get that feature.


 

Visual Studio Tip of the day – Refactoring – Changing variable names

Did you mistype a variable/function/property only to realize it in the code review and are frustrated over the time you will need to spend to correct it across the whole source code?


Visual Studio 2005 has a new feature called refactoring by which you can rename a property/function/variable at one location and the same will be replicated across all the location where the property/function/variable is referenced.


To do that, select the property/variable/function you desire to rename and right click and select Refactor > Rename. A Rename window will appear and you can select whether you want to preview the reference changes, or you want to change the entity in the comments also.


Keyboard shortcut: Ctrl R + Ctrl R


 

Is my webservice really a webservice

How to tell if your webservice is really a webservice which will interact seamlessly with external entities?


Well, keep the following in mind


1. Uses WSDL.  A Web Service should expose its service contract using WSDL.  If it can’t give you a WSDL document, it’s probably just XML over HTTP…


2. Uses SOAP.  All messages sent from and received by the Web Service must use SOAP formatting.  If it’s not using SOAP it’s probably just XML over HTTP…


3. Uses XSD.  All data types in the SOAP payload must be XSD compliant.  No platform native types are allowed.  If it’s not using XSD it’s probably just XML over HTTP…


4. Uses XML.  The underlying messages should of course be formatted using XML.


5. No Arbitrary Binary Data.  The message payload should 7 bit ASCII and should contain no embedded binary blobs.  Any binary data passed over a Web Service should be sent using either SwA, DIME or MTOM (preferably MTOM).


6. Transport is likely to be HTTP.  Although not a requirement, the majority of Web Services today use HTTP as the transport.  Compliant Web Services should definitely work over HTTP.


7. Discovery can be through UDDI.  Again although not a requirement, it should be possible to host the Web Service endpoint using UDDI.


8. Agreed Versions of Specifications.  The versions of the above specifications (WSDL, SOAP, XSD, XML, HTTP, UDDI) should be in line with the latest version of the WS-I Basic Profile (http://www.ws-i.org) – to ensure Web Service compliance between vendors.


9. Operations should be Document Style.  Operations to/from a Web Service should be Document/Message Style (e.g. SendOrder(order o)).  RPC style should be avoided (e.g. SetOrderLine1(orderId id)).


10. Should be compliant with WS-*.  Compliant Web Services should be able to accept WS-* payloads and extensions for Security, Reliability and Transactions (although not all stacks today support these yet).


Source: http://blogs.msdn.com/smguest/archive/2006/01/26/518020.aspx

How to: Determining programmatically if DLL is registered

Here is a C# code snippet to determine if a particular DLL is registered or not.


 


[DllImport("kernel32")]


public extern static int LoadLibrary(string lpLibFileName);


 


[DllImport("kernel32")]


public extern static bool FreeLibrary(int hLibModule);


 


public bool IsDllRegistered(string DllName)


{


      int libId = LoadLibrary(DllName);


      if (libId>0) FreeLibrary(libId);


      return (libId>0);


}


 


Source: http://blogs.msdn.com/asanto

Visual Studio Tip of the day – Format Document

Time and again we write code and our brackets get out of visual sync, i.e. they no longer appear as a coherent set even though they may be.


 


In Visual Studio, there is a feature known as Format Document which will align the code systematically.


 


It can be invoked by the key combination of Ctrl K + Ctrl D


 


Suppose you code looks like


 


namespace LogFileCheck


    {


    class Program


        {


        static void Main(string[] args)


        {


            TextReader sr = new StreamReader(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log”, Encoding.UTF8);


            TextWriter writesr =


                new StreamWriter(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log”,


                false,              Encoding.UTF8);


            while (sr.Peek()


                != -1)


                            {


                string line = sr.ReadLine();


                if (Regex.IsMatch(line, “&di=78″) && Regex.IsMatch(line, @”([^,]*,){19}66″))


                    writesr.WriteLine(line);}


 


                sr.Close();


            writesr.Close();


        }


    }


}


 


Press the magic keys Ctrl K + Ctrl D and voila, all your code looks pretty organized as under:


 


namespace LogFileCheck


{


    class Program


    {


        static void Main(string[] args)


        {


            TextReader sr = new StreamReader(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log”, Encoding.UTF8);


            TextWriter writesr = new StreamWriter(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log”, false, Encoding.UTF8);


            while (sr.Peek() != -1)


            {


                string line = sr.ReadLine();


                if (Regex.IsMatch(line, “&di=78″) && Regex.IsMatch(line, @”([^,]*,){19}66″))


                    writesr.WriteLine(line);


            }


 


            sr.Close();


            writesr.Close();


        }


    }


}


 


Want to format only a small selected section of the dirty code?  Select the area you want to format and press Ctrl K + Ctrl F.

Visual Studio Tip of the day – Bookmarks

How often do you wish that you could put a mark at a particular location in your source code and then switch to that point with a simple click?

With Visual Studio, you can do that with the help of bookmarks.

A bookmark is a virtual placeholder which notes the position where you place one and you can quickly go to that position from anywhere in your source files by just a few keyboard clicks.

How to define a bookmark?

To create a bookmark, press Ctrl K + Ctrl K

This creates a book mark in the code. This is indicated by a blue button like indication on the left side of the line where you placed the bookmark.

Now to switch bookmark from any portion of your code, just press Ctrl K + Ctrl N

To go to previous bookmark, press Ctrl K + Ctrl P

To unmark a particular bookmark, navigate to that bookmark and press Ctrl K + Ctrl K (yes, this is the same combination you used to create a bookmark, what you are currently doing is toggling a bookmark)

To clear all your bookmarks, press Ctrl K + Ctrl L

If you have many files in your current folder and you want to navigate to the next bookmark in the folder, the shortcuts for next bookmark in folder is Ctrl Shift K + Ctrl Shift N and the previous bookmark in folder is Ctrl Shift K + Ctrl Shift P

All the above options are also available from the menu, Edit > Bookmark > ….

If you want to see all the bookmarks in one window, Go to View -> Bookmark Window (Ctrl K + Ctrl W). And click on the bookmark you want to go to.

Happy coding.

This post is aggregated at http://msmvps.com/blogs/vipul/default.aspx 

RSS link: http://msmvps.com/blogs/vipul/rss.aspx

Atom: http://msmvps.com/blogs/vipul/atom.aspx