Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, February 13, 2010

Code Snippets in Visual Studio 2010

One of the cool features I love in Visual Studio is the Code Snippets which makes development easier in many ways. :) Let’s see how to create a code snippet and add to the snippets gallery in Visual Studio 2010. Just looking back at VS 2008, though it supports creation of code snippets it’s not an easy process without the use of 3rd party applications.
There are two snippet types…
Expansion – This type of snippet is inserted at the curser.
SurroundsWith – This type wraps around the selected code block.
Lets create  a code file header snippet.
Add a XML file to your project and name it as verHeader.snippet. (.snippet is the extension of snippet files)
image Right click on the code editor window and select the menu item Insert Snippet or Press Ctrl + K + X  and click the item Snippet.
image Remove the tag below, as we are creating a Expansion snippet.
<SnippetType>SurroundsWith</SnippetType>
Modify the Title tag to read “Code File Header”.
Next put your name within the Author tags, so are the author of your code snippet. ;)
Modify the shortcut tag with “codehead” which is the rigger word that will activate the snippet.
”Adds a header to a code file” can be the text between the description tags. And yes I love C#, so I will choose the language attribute of the code as C#.
Now we need to alter the Literal section. Literals allow you to define editable values that are inserted into your snippet. I want the you to enter your own name in the author section, so change the ID value to Author and enter your name in as the Default tag.
Code section contains what will be added when you add the snippet.
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>Code File Header</Title>
<Author>Prabath Fonseka</Author>
<Shortcut>codeHead</Shortcut>
<Description>Adds a header to a code file.</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>Author</ID>
<Default>Prabath Fonseka</Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[
/**************** Code File Header ******************
Author: $Author$
Date: 
Version:
Description:
****************************************************/
]]>
</Code>
</Snippet>
</CodeSnippet>

Above is how your final snippet should look like.
Next step is to load the snippet in to Visual Studio which we have to do through Code Snippet  Manager under the Tools menu. But by default (in VS 2010 RC) Code Snippets Manger does not comes under Tools menu so our next task is to add it under tools menu.

Go to Tools > Customize > Commands > Tools > Add Command…

By default Menu bar: radio button is selected, select Tools from the dropdown.

image Under categories select Tools and under commands select Code Snippets Manager, click OK.

image Create a folder where you can keep all your personal/custom code snippets. Then copy the verHeader.snippet to that folder. Open Code Snippets Manager, select Visual C# as language and a folder to place your custom snippets, select Imports.

image Ok, if you followed me, you have successfully created and added a code snippet to VS 2010. Wanna test? :P

Add a class file in to your project. Move the curser to the first line Press Ctrl + K + X

image
Happy Coding.

Monday, November 30, 2009

Knowledge Sharing with Peradeniya Engineering Undergraduates

As for my understanding it’s the most beautiful University in Sri Lanka, the Peradeniya University. Thanks to Microsoft Technology Evangelist Wellington and Dinesh Priyankara (MVP – SQL) I got a chance to host two technical sessions for Engineering students at Peradeniya University.

First session was an Introduction to .NET Framework and Visual Studio IDE hosted by me. Windows Server 2008 and IIS was done by Susantha Silva. Third session which was an Introduction to MS SQL Server was done by Dinesh Priyankara (MVP). Back again the fourth session was a Deep Dive session which was on LINQ (Language Integrated Query) done by me. Wellington hosted a session on Silverlight. Also not to forget, Tharindu Nanayakara explained the importance of communities-Student Champ.

Well, it was a good experience as 99% of the students were from an open source background. But I’m sure they were so impressed seeing the new UI and features of Visual Studio 2010. One thing that worries me is, I didn't have enough time to go through all the wonderful features of VS 2010.

Presentations are available to download…

Some pics of the event after the break.

SNC00106IMG_0249[1] SNC00113SNC00127 SNC00135SNC00114SNC00122

Many thanks to Chanarangana Rathnayake and all who supported to make the session a success.

Saturday, March 29, 2008

Head First C#

Four months after the release of Head First C# (by Andrew Stellman, Jennifer Greene) I was lucky enough to get my hands on this great book. Yes I said “Great”. I’ve read Head First Design Patterns and Head First Object Oriented Analysis & Design books too. It’s specially the way they present the contents to the reader attracted me to this series of books.

You don’t have to be a pro or an expert to understand the contents of Head First C#. My gesture is they’ve targeted the readers from novice to professional. Below is a sample page form this book. (I hope OReilly wont sue me doing it :D )