Thursday, February 28, 2008

Using Code Snippets

I love code snippets. They save lot of time and effort, and if used properly, will improve productivity. As explained in MSDN, you can use code snippets to type a short alias, and then expand it into a common programming construct. The code snippet list can be activated by typing CTRL + K, then X (while holding the CTRL key). Pressing TAB or ENTER after typing the name of the code snippet will insert the selected snippet at the cursor position. Using the code snippets through IntelliSense auto-completion method is more easy. All you need to do is to type the shortcut for the snippet that you want to add to your code, and press TAB, TAB. You can also use the Insert Snippet context menu in Visual Studio IDE, to insert snippets at the current cursor position.
I always felt that Surround-with Code Snippets are really nice. Using this, we can surround a block of code with a snippet, such as an exception handler. I have seen guys not writing proper exception handling blocks, because of laziness. Sometimes, we may even miss the fact that there is a special case such as a potential exception in the already written code. The surround-with code snippets are handy in such situations. To insert surround-with code snippets through keyboard shortcut, select the text to surround in the code editor and type CTRL + K, then S. Now select the required snippet from the list using mouse or by typing the name of the code snippet and press TAB or ENTER to insert the snippet. You can also insert snippets through the Edit menu by selecting Edit -> IntelliSense -> Surround With. The third method is to insert snippets by selecting the Surround With command from the context menu in Visual Studio IDE.
Few links are given below, for further reference.
Also, it will be a good idea to check John Sheehan's blog - http://john-sheehan.com/blog/ for some tips and tricks.

Wednesday, February 27, 2008

Compiling C# programs from console window

A very simple thing. The easy method is to select Visual Studio 200X command prompt from the Visual Studio Tools submenu of your studio installation in the Start -> Programs menu. Path to csc.exe (the C# compiler) will be set automatically.

If you are selecting a standard command prompt (Start -> Run -> cmd) or the Power Shell, you need to set the path to csc.exe. For that, first you should know the location of csc.exe. As you know, .NET framework has different versions, such as 1.1, 2.0, 3.5 etc. When you install a framework, corresponding folder will be created under the %SYSTEMROOT%\Microsoft.NET\Framework directory. For example, on my laptop, there are two folders - v1.1.4322 and v2.0.50727 inside the C:\WINDOWS\Microsoft.NET\Framework directory. csc.exe will be present in both folders. Once you are sure about which version to use, you need to set the PATH environment variable. To make the change temporary, execute the following command.

D:\> SET PATH = %PATH%;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

To make the path change permanent, you need to set the PATH environment variable under the system variables section. For that, select System Properties (Right click on My Computer -> Properties) and click on Environment Variables button in the Advanced tab. Select the PATH variable under the System Variables list and click Edit. Move towards the end of text that appears in the Variable Value text box, put a semicolon (;) and then add the above mentioned folder. Click OK and apply the changes. Now, whenever you fire a new command window, the path will be automatically set. The steps given above are for a Windows XP machine. There should not be any major change in other systems such as Windows 2000 or 2003.
The C# compiler has lot of options. For the complete list, click on this link. The simplest option is to specify csc {filename.cs} from the command prompt (without the { } symbols).

Monday, February 18, 2008

Java & C# - Comparison

There is a very nice article written by the great Dare Obasanjo. And as expected, the one by Frank McCown is a nice simple one. Wikipedia also has an article on the same subject. Check the links given below.

Tuesday, February 12, 2008

VB.NET & C# - Comparison

This query came from Sony (NetBIOS). He was looking for an article, which compare keywords in C# and VB .NET. First I thought of creating a list myself. I started with little bit of googling and found that a couple of very decent articles are already there. The one written by Frank McCown is really good. The MSDN article is also nice, in the sense it compares the keywords in different languages. The links are given below.
2. http://aspalliance.com/625 (Derivative of the above one)