Two simple tricks to speed development

I just wanted to post two simple keyboard tricks to speed up development when you are using the Visual Studio 2010 IDE. Both have to do with getting the information you need quickly when developing within the Source page.

First, you can easily flip from the Source page to its Code Behind file by hitting F7. You can also see the Designer page by hitting Shift+F7.

Second, you can access the Properties window of any control by placing your cursor on the control and hitting the F4 key.

I will post more navigation tricks like this in the days to come.

Surround With in C#

As I mentioned in yesterday’s article entitled Inserting snippets in C# you can also insert snippets around existing code. To do this you need to use the Surround With option.

These snippets can be accessed on the design page (where you will receive snippets of HTML and ASP.NET code) or from the code behind page where you can get C# snippets.

You can access these snippets in several ways:

  • Highlight the text that you want the snippet to surround, right-click and select Surround With
  • Highlight the text that you want the snippet to surround and hit CTRL+K, CTRL+S, The same dialog box as above appears
  • Highlight the text that you want the snippet to surround and on the Menu Bar browse to Edit–>Intellisense–>Surround With

Use Ctrl + Period to resolve a missing namespace reference

I love Microsoft Intellisense and the fact that it is so helpful. One aspect I like is that it will help you resolve a missing namespace reference. Generally when you add a reference to a namespace and it has not been declared, Microsoft will underline the missing reference in red. When you hover over it you will get an error message saying : “The type or namespace name ‘xxx’ could not be found (are you missing a using directive or an assembly reference?)

 missing reference

 

 

To resolve this problem you have two options, you can right-click the missing reference and choose Resolve.

missing ref 2

You can place a reference to the missing namespace at the top of your class by choosing the using statement or you can place it inline by selecting the full namespace. If you are going to be using this namespace reference in other places within your class then obviously the former is the better choice.

There is another way to create a reference to this missing namespace. When you know the namespace is missing and you have finished typing the initial reference, you can hit Ctrl + . (period) to raise a dialog box helper. From here you can arrow up or down to select and instantiate the namespace reference. This way you do not have to reach for the mouse and break your flow.

missing ref 3

Windows Phone 7 and FREE ebooks

After attending a Windows Phone 7 HackFest in Toronto on the weekend I have become enamoured with all things mobile. I love the fact that I can create a simple app in little to no time and then test it on my Samsung Focus for some instant gratification.

This being said, there is still a way to go in my studies to become really adept at coding for the mobile platform. Fortunately there are some freebies out there that can ease the pain. What follows is a list of free ebooks that are being distributed on the web right now to get you started in coding for Windows Phone 7.

The first to arrive when the platform was announced was Programming Windows Phone 7, by Charles Petzold
This book is really in-depth and will give you an extensive overview of how to do most things with the phone.

If however time is short, then check out these books by Rob Miles. Miles is a well-known lecturer at the University of Hull in the UK and is also a Microsoft MVP. For years he has created the C# Yellow Book for incoming students to help them learn the language. He has now created the Windows Phone Blue Book which is just as useful. These two books along with information on developing apps using XNA for the Windows Phone are included in the link above. There are even slides for teachers who wish to teach the curriculum to their students.

Using Statements to wrap your database connections

In yesterday’s piece entitled Using Regular Expressions (Regex) to find all your SqlConnection strings I wrote about the fact I was trying to use  a regex string to find all my SqlConnection declarations.

In the piece I made mention of the fact that the using statement are becoming the defacto way of wrapping your connections. I first heard about this in the James Michael Hare series of articles C#/.NET Little Wonders – All The Wonders In Their Glory

The using statement is not the same as the using declarations at the top of your code behind files. Instead, they are used to wrap pieces of code so that they will call IDisposable as soon as they go out of scope.

So, instead of forgetting to call dispose on your SqlConnection instances and running the chance of having some connection pooling issues, we can use the following to instantiate our objects:

using statement

As you can see I have wrapped the code in a using block with both brackets and curly braces. I don’t have to use any try/catch statements nor do I have to explicitly dispose of my connection object. The using statement will call the Dispose() method as soon as it is out of scope – either when it completes or if it encounters an error.

So, check out using statements. They will make sure you never have to worry about closing out your connection strings and they will make your code a little bit neater.

Using Regular Expressions (Regex) to find all your SqlConnection strings

I was looking through some inherited code and there were some connection strings spread out throughout the project. Now because I try to make sure everything is closed in good order I needed a way to find where each was declared. I could change them all to using Statements but that just seems like a lot of work.

Instead, I found this nifty little piece of regex syntax that allows you to search through your connection strings:

new:Zs*(System.)*(Data.)*(SqlClient.)*SqlConnection(\([0-9a-zA-Z]*\))

In Visual Studio 2010 hit Ctrl+F and place the string in the Find what: textbox of the Find and Replace dialog box. Make sure to click the Use checkbox at the bottom and select Regular expressions. Also, make sure there are no trailing spaces after the last bracket or else it will not work. Click Find Next and voila. The nice thing about this is that it will find them if they are declared explicitly or not.

Find SQlConnection string with regex

Follow

Get every new post delivered to your Inbox.