0

Stored Procedure’s

Posted by Mark on June 16, 2011 in Database

I am a Microsoft developer (.Net primarily) and I try to stay away from things to do with databases. However, the project I am currently on is using stored procedures. When I first joined the project I had no idea what a stored procedure was so I thought I would look into them. Below is what I have found out from my research (and why I will be using them in future).

Note: Within this piece I talk about creating a stored procedure and I will be using MSSQL. You can create stored procedures in MySQL and Oracle however the syntax is slightly different, but with a Google search you will find out how to create them easily enough.

What are stored procedures?

Stored procedures are a mechanism to simplify database development by allowing you to group SQL statements into manageable blocks. Stored procedures are extremely similar to the construct seen in other programming languages. They accept data in the form of input parameters that are specified at execution time and they can return data. Data commonly returned from a stored procedure are recordset’s and return codes ( a stored procedure does not have to return anything).

Why should you use stored procedures?

Stored procedures have three main advantages for you to use them, the advantages are as follows:

Abstraction

  • Stored procedures can be used by multiple users and client programs, if you utilise them in a planned manner, you will find that your development cycle can take less time.
  • Stored procedures allow for a centralised place for maintaining code. If you had a select statement that is used in multiple places without the use of stored procedures you would need to find each instance of it and update the SQL statement. However, with the use of a Stored Procedure you can simply update it within the one location; this can lead to saving time in the development process.

Security

  • You can grant users permission to execute Stored Procedures independently of underlying table permissions. Therefore allowing for more of a secure setup.

Performance

  • Stored procedure are compiled once and then reutilised. This results in tremendous performance boosts when stored procedures are called repeatedly. If you were using standard SQL statements within you code the compilation of them happens each time they run.
  • Stored procedures can reduce long SQL queries to a single line that is transmitted over the wire. This allows you to reduced network usage between clients and servers. The more SQL statements you group together within a stored procedure, the more you reduce the network usage and the length of database locks.
  • Applications that use stored procedures have access to increased memory and disk space on the server computer.

An example of a stored procedure

Take the following SQL statement that you may have within multiple pages

SELECT * FROM CustomerList WHERE Email LIKE ‘mark%’

Within a stored procedure the statement would look as follows.

CREATE PROCEDURE sp_GetCustomersByEmail

@email VARCHAR(255)

AS

SELECT * FROM CustomerList WHERE Email Like @email + ‘%’

To call this stored procedure within the SQL manager you would simply do the following.

exec sp_GetCustomersByEmail @email = ’mark’

Now you may be thinking “that seems like a lot of work to make a stored procedure for a simple command”, but what you need to think of is the following. You have just realised using SELECT * is a big drain on the performance and you only ever use the name, email and telephone number from the customer list when you use the statement. If you are using the stored procedure you simply have one place to update the statement, however, if you were not using a stored procedure, you would have to go through every location where that SQL statement is in use and update it. As you can see the initial time investment in creating the stored procedure is not looking so bad considering the alternative you could face.

To call a stored procedure in code you would do the following:

Note: This code assumes you know how to start a new connection and execute a command. It is also in C#.net

// 1. Setup your connection

// 2. create a command object identifying the stored procedure

SqlCommand cmd = new SqlCommand(“sp_GetCustomersByEmail”, conn);

// 3. set the command object so it knows to execute a stored procedure

cmd.CommandType = CommandType.StoredProcedure;

// 4. add parameter to command, which will be passed to the stored procedure

cmd.Parameters.Add(new SqlParameter(“@email”, userEmail));

// 5. Execute the command

// 6. Read the data received from executing the command

That is it for this post; hopefully you have taken something away from it and if you have any questions/comments leave a comment at the bottom of the page and I will get back to you.

Tags: , , , , ,

 
0

Silverlight with Windows Phone 7 – An introduction

Posted by Mark on June 16, 2011 in Silverlight, WindowsPhone7, WPF

In this tutorial I aim to introduce you to Silverlight development on Windows Phone 7. To help achieve this aim we will create a simple application that will allow you to get the BBC News Top Stories RSS feed and display them for easy browsing. It will also include the ability to click on a story of interest to read it in full on their website.

Requirements

To develop with Silverlight for Windows Phone 7 you will need a few things installed.

NOTE: You should install these applications in the order they are shown below.

You will also need:

  • A knowledge of WPF.
  • A knowledge of how to use visual studio (creating projects, adding classes etc).
  • A knowledge of C# will be helpful but is not a major requirement as it is pretty easy to learn.

Creating the Application

Now you have all the tools you need installed you’re ready to get developing.

Open visual studio 2010 and create a new Windows Phone 7 Application.

Image 1

image 2

Now we have a project set up you should be presented with a screen similar to this.

Image 3

The left panel shows you visually how your application looks through a WYSIWYG editor and the right panel shows you the XAML editor that creates what you are seeing on the left. To add controls and change properties you can use either of the panels. For adding controls using the left panel you use the Toolbox and for adding controls using the right panel you type them out. We will be typing the controls out into the XAML editor directly.

Moving Along

Now everything is setup and you understand what you are looking at the first thing we want to do is add a reference to the project. The reference we need to include is System.Xml.Linq, this will be used later when we start actually writing some code.

The next thing to do is to remove the text at the top of the application that says “MY APPLICATION”. To do this, simply click on the text in the left panel and hit delete. When you deleted the control you may have seen the line related to the control be removed from the XAML in the right panel, if not don’t worry it is not important I am just pointing out that whatever you do to one of the panels the operation is reflected in the other.

Seeing as we are going to be using the XAML editor (the right panel) instead of the WYSIWYG editor (the left panel) we may as well make the XAML editor nice and big and hide the WYSIWYG editor.

To do this locate the button that looks like this Image 4

It can typically be found here (highlighted)

Image 5

After you have clicked it the window will now look as follows.

Image 6

Notice that I have highlighted buttons on the right hand side of the above image. To view the XAML editor simply click the one that is not currently selected (see image below for what the button will look like)

Image 7

By using the two buttons that were highlighted above you are able to flick back and forth between the WYSIWIG editor and the XAML editor.

So we now have the XAML editor front and centre, so we can now add our List Box control. To add this we first need to find the correct location we want it to show. We will add it into a pre-existing grid that was created when we created the project, so in the XAML editor look for this line of XAML.

<!–ContentPanel – place additional content here–>
<Grid x:Name=”ContentPanel” Grid.Row=”1″ Margin=”12,0,12,0″></Grid>

Now you have located the line we will add our List Box control. To do this we need to add the following XAML between the Grid tags.

<ListBox x:Name=”lstFeed”>

</ListBox>

Your XAML should now look like this

<!–ContentPanel – place additional content here–>
<Grid x:Name=”ContentPanel” Grid.Row=”1″ Margin=”12,0,12,0″>

<ListBox x:Name=”lstFeed”>

</ListBox>

</Grid>

Ok so if we were to compile and run this now we would not see anything apart from the text at the top of the application that says “page name”. This is because we need to actually add some content to our List Box.

So if we right click within the XAML editor panel and select “View Code” the C# code that supports our application will be shown to us.

Image 8

Do you remember adding the reference to System.Xml.Linq earlier?  Well this is where you need to use it. All you need to do is add using System.Xml.Linq at the top of the page (where the rest of the using statements are declared).

We now need to create a new class that will hold news feed item’s. Add a class to the project called NewsItem.cs. The class should look like so.

public class NewsItem
{
//GUID of the news item.
public string GUID { get; set; }

//Title of the news item.
public string Title { get; set; }

//Description of the news item.
public string Description { get; set; }

//Web link to the news item.
public string Link { get; set; }

//Date and time the news item was published.
public string Date { get; set; }

//Image location for the news item’s thumbnail image.
public string Image { get; set; }
}

Now that we have a class to take care of our news items we can go back to the main application code and get cracking.

Firstly we will create the WebClient, the WebClient is used to download the RRS feed. The following code needs to go in to the constructor.

//WebClient object that will be used to download the RSS feed.
WebClient getFeed = new WebClient();

//Add the event handler that will be called when the download has completed and the RSS feed has been downloaded
getFeed.DownloadStringCompleted += new DownloadStringCompletedEventHandler(getFeed_DownloadStringCompleted);

//Start the download on the URL that is passed to it (this is the BBC News Top Stories feed)
getFeed.DownloadStringAsync(new Uri(“http://feeds.bbci.co.uk/news/rss.xml”));

We now need to add the function that will be called when the DownloadStringCompleted event is fired. To do this the following code needs to be added.

void getFeed_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
If(e.Result != null)
{


}
}

Now we have the skelton structure of the event, we now need to put the code in that will actually do the work.

You will have noticed in the above function that there is an if statement that checks to see if e.Result is not empty. We have this check to make sure that the download went according to plan and actually got some data. It is within this if statement that we will add the rest of the code needed for the application.

NOTE: This if statement does not check to make sure the data is exaclty what we expected, a future enchancement can be made here to verify the data (look at the “Wrap Up” section for ideas of how this program could be improved).

The following code should be added between the if statement stated above in the getFeed_DownloadStringCompleted function.

//Create the XDocument by passing in the result string we received (this //is the actual XML of the BBC’s RRS feed).
XDocument doc = XDocument.Parse(e.Result);

//As the BBC’s RRS feed uses namespaces for images we need to create this //namespace variable for use in the linq query.
XNamespace serv = “http://search.yahoo.com/mrss/”;

//Perform a linq search on the XML document to get all the news stories //that have a thumbnail image width of 66px. The constructed data is //stored in the data variable.

var data = from query in doc.Descendants(“item”)
where (int?)query.Element(serv+”thumbnail”).Attribute(“width”) == 66 //Casting as an int? allows the query to //deal with null values
//Create a new NewsItem for each record that is found
select new NewsItem
{
//Setup each of the properties by getting there corresponding
//element from the XML.
GUID = (string)query.Element(“guid”),
Title = (string) query.Element(“title”),
Description = (string) query.Element(“description”),
Link = (string) query.Element(“link”),
Date = (string) query.Element(“pubDate”),
Image = (string)query.Element(serv+”thumbnail”).Attribute(“url”).Value??”" //If the image is null then just //write a default empty string.
};

//Bind the data retrieved from the XML document to the List Box we created
//earlier.
lstFeed.ItemsSource = data;

So we are good to go right? Not quite, if you run the app now you would get a result similar to this.

Image 9

You may be think “Wait, where are the actual news stories and images?” It’s quite simple really, currently we are passing in a list of news items and the List Box control has no idea how to display them so it just display’s the object type of what they are.

To correct this issue we need to go back to the XAML editor and add a List Box Item Template. By doing this when we pass a NewsItem object to the List Box it will know how to display correctly.

Firstly we need to find the List Box XAML we added previously (we called it lstFeed). Now that we have located it we need to add the following code in between the open and close tags.

<!–State that we are making an item template–>
<ListBox.ItemTemplate>
<!–Declaring the data template gives use the ability to replace the visual appearance of the default data item (which is just one text block which performs a ToString() operation on the objects you pass to it; This is why we received the result above when we tried to run it previously as it used the default data template)–>
<DataTemplate>

<!–The stack panel in WPF is used to stack its child elements below or besides each other depending on its orientation. Below it is stacking them vertically by default–>
<StackPanel>

<!–A grid allows you arrange it children controls in a tabular structure of rows and columns. It is similar to a HTML table but it is more flexible. Here we set up a grid that will hold the needed controls to display our news items.–>
<Grid Width=”480″>

<!– Here we define the columns and there widths. One column is for the image, the other for the text controls. Column definitions start as 0 so the first column is 0 the second is 1 etc–>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”66″></ColumnDefinition>
<ColumnDefinition Width=”400″></ColumnDefinition>
</Grid.ColumnDefinitions>

<!–Here declare the control that will show the image and bind the Image property from the new item class to it. Whenever a news item object comes in it will know that the source is to be taken from the Image property. We also set the width and height of the control and tell the control to show in the first column of our grid–>
<Image Name=”txtImage” Source=”{Binding Image}” Width=”144″ Height=”81″ Grid.Column=”0″ ></Image>

<!–Here we create a new Grid that will hold the controls to display the text of the news item. We also assign it to be placed in the second column of the initial grid we created (remember the columns start at 0 so 1 is the second column.–>
<Grid Grid.Column=”1″>

<!–Create the needed row definitions for the controls. The rows height is declared as Auto so it can take as much or as little as it needs. The rows work the same way as the columns in the fact that they start at 0, so the first row is 0 and the second is 1 etc–>
<Grid.RowDefinitions>
<RowDefinition Height=”Auto”></RowDefinition>
<RowDefinition Height=”Auto”></RowDefinition>
<RowDefinition Height=”Auto”></RowDefinition>
<RowDefinition Height=”Auto”></RowDefinition>
</Grid.RowDefinitions>

<!–This is a hyperlink button control, we use it to display the title of news items and also to link to the actual news article on the BBCs website. Here I have set it to open in a new browser window and made the text align to the left as well as setting the font to be 22px. Again I perform binding for the content it shows (like what we done for the image control) but this time we bind the Title as the text to show and we also have another bind that binds the link to the navigation uri. We also assign the control to the first row and have the height set to auto and the width set to 390 –>
<HyperlinkButton Name=”txtDescHyper” Margin=”0″ TargetName=”_blank” HorizontalContentAlignment=”Left” FontSize=”22″ Content=”{Binding Title}” Grid.Row=”0″ NavigateUri=”{Binding Link}”  Height=”Auto” Width=”390″></HyperlinkButton>

<!–This is a text block control and we use it to show the description of the news item. Again I perform a bind so the text that is displayed is the description. I also set the control to be assigned to the second row and set the text wrapping property to wrap, this allows the text block to be multiline. The height, width and font size are also set.–>
<TextBlock Name=”txtTitle” Text=”{Binding Description}” Grid.Row=”1″ TextWrapping=”Wrap” Height=”Auto” FontSize=”20″ Width=”390″></TextBlock>

<!–This is a text block control and we use it to show the date that the news item is from. I perform a bind here so the text that is displayed is the date of the news item object and I assign the control to show in the third row. I also assign the height and width and text wrapping property again.–>
<TextBlock Name =”txtDate” Text=”{Binding Date}” Grid.Row=”2″ TextWrapping=”Wrap” Height=”Auto” Width=”390″></TextBlock>

<!–This closes the grid that contains the display controls–>
</Grid>

<!–This closes the grid the grid that encompasses all of the controls and the secondary grid.–>
</Grid>

<!–Here I create a line to add a space between each news item object. This is done so the stories don’t look as if they just merge into one.–>
<Line Name=”seperator” Fill=”AliceBlue” Height=”10″ Width=”400″ Visibility=”Visible”></Line>

</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>

Now if we run this you should hopefully see something similar to this:

Image 10

All that is left to do now is to change the text at the top of the page to something more appropriate. To do this look at the XAML editor and find a control named “PageTitle” then simple replace its text properties contents with “BBC News Feed” and set its font size to 64 so the new text is fully visible.

Now if you run the app you should have something similar to this:

Image 11

Wrap Up

As you have seen it is very easy to get started with Silverlight on Windows Phone 7 and you now have a simple application to read the feeds of the top news stories from the BBC. However, there is room for improvement.  I hope you have learned something from this tutorial and please feel free to leave questions in the comments section.

Below are some thoughts on what you could do to improve the app:

  • More error checking and validation. At the moment the application is very barebones and a number of possible errors lay in wait. To combat this checks should be done throughout the code to ensure the integrity of the data being loaded.
  • Limit data usage. Currently the application makes a request for the RSS feed every time you enter the application,  even when you go to read a full news story and come back to the application. To combat this you can investigate tomb stoning and saving to isolated storage.
  • Make it use a panoramic control so you can show many different types of RSS news feeds. With this a user could simply swipe left and right to be presented with new information related to more specific news feeds such as Technology, UK, England etc. The BBC has many RSS news feeds so take a look to get some more information. (http://www.bbc.co.uk/news/10628494)

Tags: , , , ,

 
0

Riddle Me Some Stats….

Posted by on December 8, 2010 in General, Projects

Dam that was such a poor title, unfortunately I can’t think of anything more quick witted.

Anyway to the main point of this post.

At the beginning of November I put up a little game to the Windows Phone 7 marketplace and in my opinion the game was crap. It was a rushed job so I could just get verified so I could start deploying my real games to my phone for performance testing. Recently Microsoft has just published stats on apps so you can see the sales data of your application and I was expecting to see 100 downloads if I was lucky, what i saw was in fact shocking, in a good way.

Over a one month period my App was downloaded 4,657 times. This astonished me, granted my application was free which most likely helped it. But for an app that took me 30 minutes to write top whack I feel pretty pleased with myself.

Listed below are the numbers.

Daily Downloads

Cumulative Downloads

Country Specific Data

What I have learned!
From this experience I have seen that an app that is enjoyable does not need over the top graphics, but I do realise they play a vital part to keeping your clientele and drawing more in. So when I go home tonight my other projects are being placed on hold and I am focusing on version 2.0 of my application. It will have more content and I will make it look nicer as well.

My application is called Riddle Me This if you fancy having a play with it (and help my stats some more :) ). I will update on the blog and Twitter when the new version is out, which hopefully should not be too long as the boost these stats have given me has really motivated me to continue.

If you have played Riddle Me This or end up playing it, let me know what you think and suggest some improvements and I will aim to deliver.

Until next time. You stay classy internet.

Tags: , , , , ,

 
2

Windows Phone 7 – My Impression!

Posted by on November 21, 2010 in General, Life

So, I have had my windows phone 7 for around two weeks now and I thought I would do a little post with my thoughts on the phone that I got and also the OS.

The phone

I purchased the HTC HD7 on a 24 month contract from O2. I got the HD7 as at the time of writing, it has the largest display on a WP7 phone and the storage size is one of the largest as well (16GB). Overall the phone is very nice; however, some have said it feels a little heavy, but I prefer a phone that has a little weight behind it (as it reminds me that I do actually have it in my pocket).

The screen is very responsive and the imagine quality is very nice (the Samsung Omnia 7 is by far the best quality screen though, with regards to the quality of the image it produces). The buttons that are located on the device feel nice to use and the flush design of the three main buttons on the front of the phone (back, start and search) make for a very aesthetically appealing design.

The onboard 5 MB camera with dual flash takes a nice photo for a camera phone and the 720HD video that it can record is very nice as well.

A feature that I do like is the kickstand at the back of the phone, so you can have it deployed and watch your device hands free.

The OS!

The main thing to state straight away is that Microsoft is back in the game with their latest Mobile operating system.

The system feels sleek and is very user friendly. At a Microsoft event I attended they stated that they took inspiration for the user experience from the metro system (underground to us English folkJ) and that can be seen right away.

The tiles are straight to the point and you can typically make an educated guess of what each specific tile will do. The GUI is not over powering and it lets you navigate around the phone with ease, and by quickly glancing at the phone you can quickly distinguish the different areas and get to the section you want to go to in a jiffy.

The performance of Windows Phone 7 is very good; you can quickly navigate between screens and do various other actions instantaneously. The smooth transitions that take place and the little things that some may not notice (like automatic icon rotation within the app bar when you flip the phone from portrait to landscape view) make your experience with Windows Phone 7 more enjoyable.

Changes

Although I am very impressed with what Microsoft has produced within Windows Phone 7 I feel that a couple of areas have been overlooked. The improvements that I feel  are need to be done are as follows:

Folders – At the end of the day on either of the two screens you have at your disposal you are presented with two lists, and overtime these lists have the possibility of getting very long, especially once you have had your phone for a couple of months. I feel that a needed feature is the ability to make folders in which you can group your applications. This would therefore save on screen space and make it easier for users to navigate their phones.

Quicker accessing of alphabetical lists – This feature needs to be implemented within the contacts list and within the secondary screen that you access in Windows Phone 7 by swiping to the left (I suppose you could call it the “all programs” list really). These lists are sorted alphabetically and although the contacts section does allow you to do a search sometimes I just want to be able to hit a “b” and be taken to all my contacts that are organised under “b”, this is also true for the “all programs” list. A simple run down for “a” to “z” on the side of the screen is all that is needed and it would allow for a much faster browsing experience. (SEE EDIT SECTION BELOW)

Emails – At the present moment each email account you have is displayed as a different icon on the start page. I believe this needs changing so that only one icon shows and that all email accounts are grouped together (unless the user specifies that they want specific ones to have a dedicated tile).   This problem could be addressed through the implementation of folders to allow the user themselves to organise their email accounts into the designated structure they want. However, I believe a redesign of how the email system works could be more beneficial and make the experience a lot more pleasant to work with.

Cut, Copy and Paste – An obvious feature needed is the ability to cut, copy and paste. I do believe this is coming out within their January update and I am just hoping that it is implemented in the correct fashion so it is easy to use.

Adobe Flash Support – I am hoping that support for Adobe Flash Player is integrated within the next update to Windows Phone 7, although I am not holding my breath. But it would be nice if it turned up on Windows Phone 7 sooner rather than later.

HTML 5 Support - By how much Microsoft has been pushing HTML 5 (through IE9) I am surprised that Windows Phone 7 was not released with a web browser that supports it. Therefore, I am hoping that they release a version of IE9 for Windows Phone 7 within the upcoming update.

Apps – Some of the apps that exist within the marketplace just seem rushed and could do with a little more polish. Take for example the official Twitter application, on the iPhone this application is very polished and works extremely well. However, on Windows Phone 7 it feels as if they slapped the application together within a day and said “Yeah that’s fine we’ll come back to this in a while”. Developers need to learn that half baked solutions do nothing for them, if anything it puts people off (unless they are honest with their users and say that the app is within a beta phase or something to that effect). Technically this is nothing to do with the operating system although it does have the possibility to damage the community around Windows Phone 7.

These are just my personal opinions of what I feel about the HTC HD7 and Windows Phone 7. If you want to leave a comment to tell me what you think.

EDIT: After a comment from Dave I went to try what he suggested but i could not find it. However, I did notice that if you are within the People area and you hit any of the letters it brings up a screen where you can select from “A” to “Z” and be taken to that section. I don’t Know why I never tried that before XD. (This information is also true for when you access your contact list through the normal phone tile)

Tags: , , , ,

 
0

Tips N Tricks

Posted by on September 13, 2010 in C#, C++, General, Uncategorized

A couple of weeks ago I went to a conference called Guathon. It was an event sponsored by Microsoft to tell developers about Visual Studio 2010, ASP.net  4, MVC 2 and some other technologies. Within the presentations that were given MVC 3 was introduced along with the new razor view engine for ASP.net. Many improvements have been made to MVC 3 to streamline the development process, with a very big emphasis put on rebuilding and deploying code faster.

However, I digress as the post is called “Tips N Tricks” and not “MVC 3 and the Razor view engine”. As Visual Studio (VS) 2010 is being adopted by more developers, it is important to know how we can utilise it effectively to get the most out of the IDE. Although some of you reading this will probably know some of the tips and tricks that can be done within the IDE not everyone does and hopefully you will at least learn one thing from this.

A quick side note is that although ReSharper offers most of this functionality but it is important to know that Visual Studio 2010 offers this out of the box and therefore allows you to get rid of clunky add-on’s and stops you from spending a fair bit of money.

WARNING: this post is biased towards using VS 2010 with C# and C++ but I imagine the tips and tricks discussed should work with the majority of environments.

IntelliSense

Within VS 2010 the IntelliSense search is performed with a fuzzy search (meaning that possible relevant answers to your search arguments are shown).

An example of this is if we wanted to find HttpStyleUriParser typing this out each time is quite mundane but with IntellisSense’s fuzzy search typing in just “h” shows a list of possible matches that we can scroll through to find the correct one.

Image 1

However, if we type “HSUP” (the first letter of each world within HttpStyleUriParser) we generate a much shorter list to be able to select our desired object from.

Image 2

The accuracy of the ” HSUP” style search does differ depending on whether or not you have other classes, methods or variables with the same name styling. An example of this could be seen if you had one class called HelpFunctions and another called HelloFoo, within this instance both option would be shown within the drop down box of possible options whe you enter HF.

Image 3

Box Selection

I don’t think box selection is the correct name for this, however, due to the lack of a better name this is what I am calling it [:)].

This little feature is useful for when you need to change something in bulk; for example we have declared 5 variables:

Image 4

But we realise these should not be of type int they be of type string, sure you can click on each one and rename them or you can perform a box selection (I bet you never saw that coming [;)]).

To perform a box selection you simple click just before the first “i” on the first row you want to select (or click just after the last “t” on the last row you want to select) and hold the mouse button down and
then you hold the alt key and simply drag the mouse down (or up) to select all the properties you want to change. Once the selection has been made you can then just start typing to replace “int” with “string“.

Image 5

(Another way to perform this action is by clicking just before the first “i” on the first row you want to select and then holding the Shift key and Alt key and clicking just after the last “t” on the last row you
want to select.)

Collapsing and Expanding Methods

When classes get a little on the large size it can be a pain to scroll through them to find the method you want. However, that need not be a problem anymore with the help of ctrl+m+o and ctrl+m+l.

By pressing ctrl+m+o you can collapse all of the methods that you have within your class so you can then browse through the class to quickly find the method you want.

Image 6

Then with the press of ctrol+m+l you can quickly expand all of the methods again to see then in full detail.

Image 7

Block Comments

If you have a large chunk of code that you want to quickly comment out you have multiple options you can go through.

  • Comment each line:

//int m_demo1;

//int m_demo2;

  • Do a block comment

/* int m_demo1;

int m_demo2;

*/

Or use a keyboard shortcut.

To use the keyboard shortcut you simply select the block of code you want to comment out and then press ctrl+k+c.

Image 8

To uncomment the block of code you simply select the block of code and press ctrl+k+u.

Image 9

Move between open tabs

When you have multiple tabs open within VS 2010 it can me quite difficult to find the tab that you want, but by pressing ctrl+tab you can scroll through your open tabs and find your desired tab more
efficiently. Once you have pressed ctrl+tab a box similar to the one below is shown.

Image 10

By then hitting the tab key you will cycle through the open tabs and the highlighted tab will change within the box. Once you release the keys it will go back to the main IDE with the selected tab being shown. It is important to note that you need to keep ctrl held or the box disappears, tapping the tab key will cycle through the open tabs.

When this box is open you can also just click on the file you want to show, instead of pressing the tab key to cycle through them.

Auto complete of statements

For each type of statement that can be generated (such as for, foreach, while, try catch, if etc) visual studio 2010 lets you auto create templates of them with dummy information filled in ready for you to
change.

For example a foreach statement would look like the image below.

Image 11

As you can see there are three sections that you can change; the type of the object, the name you want to give each item that is pulled from the collection and the collection to loop through.

  1. To create a template statement you type the statement name you want and then hit the tab key twice.
  2. The first section to change is the data type that the item should be. Once you have typed that in you then hit the tab key (when you hit the tab key it moves over to the next editable parameter).
  3. The next section to change is the name you want to give the item. Once you have typed that in you then hit the tab key again.
  4. Finally the last section is the collection you want to use. Once you have typed that in you hit the enter key and your foreach statement is ready to use.

Image 12

Incremental Search

Incremental search allows you to perform a quick search within the currently selected class you have open.

To use it all you do is press ctrl+i and then start typing, as you type results matching your search text are highlighted within the editing area. Note that what you are typing appears in the
bottom left of the windows in the status bar area and not in any form of popup window.

To exit incremental search you just hit the escape key (or click the left mouse button inside the editing area).

View Call Hierarchy

Sometimes you want to know where a specific method within a class is being called. You could do this by putting a break point within the method in question and then tracing your program through to see where the code returns to. However an easier option is to use the call hierarchy.

Within the call hierarchy calls to your selected method and calls from your selected method are shown. This allows you to quickly jump to the sections of your code where your selected method is being
called from.

To view the call hierarchy of a method simple select the method you want to view and press ctrl+k+t alternatively you can right click on your selection and select “View Call Hierarchy”. Once you have done this the call hierarchy will be shown and look something like this

Image 13

By double clicking on RandomFunction() you would be take to the method called RandomFunction().

That is all you’re getting from me for now, as there are so many little tips and tricks books could be written on them (besides I think I have wasted enough of your time).

Thanks for reading the post and hopefully you did learn something new.

Mark

Tags: , , , , , , , ,

Copyright © 2009-2012 Mark William Davies All rights reserved.
Desk Mess Mirrored v1.8.5 theme from BuyNowShop.com.