Blog Moving

I’m moving this blog over to http://mikefourie.wordpress.com. The www.freetodev.com url should redirect there soon. I’m busy copying over some posts and formatting the new site. I’ll be leaving this site up for now but all new posts will be to the new blog.

Hoping to do some interesting blogs soon…

Mike

Posted in Uncategorized | Leave a comment

PowerCommands for Visual Studio 2010

The RTM for PowerCommands for Visual Studio 2010 has just shipped! I found this an invaluable tool in VS 2008 and it’s good to have it back.

Download

If you’ve not used it before, below is a summary of what is on offer in version 10.0:

Enable/Disable PowerCommands in Options dialog
This feature allows you to select which commands to enable in the Visual Studio IDE. Point to the Tools menu, then click Options. Expand the PowerCommands options, then click Commands. Check the commands you would like to enable.
Note: All power commands are initially defaulted Enabled.
Format document on save / Remove and Sort Usings on save
The Format document on save option formats the tabs, spaces, and so on of the document being saved. It is equivalent to pointing to the Edit menu, clicking Advanced, and then clicking Format Document. The Remove and sort usings option removes unused using statements and sorts the remaining using statements in the document being saved.
Note: The Remove and sort usings option is only available for C# documents.
Note: Format document on save and Remove and sort usings both are initially defaulted OFF.
Clear All Panes
This command clears all output panes. It can be executed from the button on the toolbar of the Output window.
Copy Path
This command copies the full path of the currently selected item to the clipboard. It can be executed by right-clicking one of these nodes in the Solution Explorer:
The solution node; A project node; Any project item node; Any folder.
Email CodeSnippet
To email the lines of text you select in the code editor, right-click anywhere in the editor and then click Email CodeSnippet.
Insert Guid Attribute
This command adds a Guid attribute to a selected class. From the code editor, right-click anywhere within the class definition, then click Insert Guid Attribute.
Show All Files
This command shows the hidden files in all projects displayed in the Solution Explorer when the solution node is selected. It enhances the Show All Files button, which normally shows only the hidden files in the selected project node.
Undo Close
This command reopens a closed document, returning the cursor to its last position. To reopen the most recently closed document, point to the Edit menu, then click Undo Close. Alternately, you can use the CtrlShiftZ shortcut.
To reopen any other recently closed document, point to the View menu, click Other Windows, and then click Undo Close Window. The Undo Close window appears, typically next to the Output window. Double-click any document in the list to reopen it.
Collapse Projects
This command collapses a project or projects in the Solution Explorer starting from the root selected node. Collapsing a project can increase the readability of the solution. This command can be executed from three different places: solution, solution folders and project nodes respectively.
Copy Class
This command copies a selected class entire content to the clipboard, renaming the class. This command is normally followed by a Paste Class command, which renames the class to avoid a compilation error. It can be executed from a single project item or a project item with dependent sub items.
Paste Class
This command pastes a class entire content from the clipboard, renaming the class to avoid a compilation error. This command is normally preceded by a Copy Class command. It can be executed from a project or folder node.
Copy References
This command copies a reference or set of references to the clipboard. It can be executed from the references node, a single reference node or set of reference nodes.
Paste References
This command pastes a reference or set of references from the clipboard. It can be executed from different places depending on the type of project. For CSharp projects it can be executed from the references node. For Visual Basic and Website projects it can be executed from the project node.
Copy As Project Reference
This command copies a project as a project reference to the clipboard. It can be executed from a project node.
Edit Project File
This command opens the MSBuild project file for a selected project inside Visual Studio. It combines the existing Unload Project and Edit Project commands.
Open Containing Folder
This command opens a Windows Explorer window pointing to the physical path of a selected item. It can be executed from a project item node
Open Command Prompt
This command opens a Visual Studio command prompt pointing to the physical path of a selected item. It can be executed from four different places: solution, project, folder and project item nodes respectively.
Unload Projects
This command unloads all projects in a solution. This can be useful in MSBuild scenarios when multiple projects are being edited. This command can be executed from the solution node.
Reload Projects
This command reloads all unloaded projects in a solution. It can be executed from the solution node.
Remove and Sort Usings
This command removes and sort using statements for all classes given a project. It is useful, for example, in removing or organizing the using statements generated by a wizard. This command can be executed from a solution node or a single project node.
Extract Constant
This command creates a constant definition statement for a selected text. Extracting a constant effectively names a literal value, which can improve readability. This command can be executed from the code editor by right-clicking selected text.
Clear Recent File List
This command clears the Visual Studio recent file list. The Clear Recent File List command brings up a Clear File dialog which allows any or all recent files to be selected.
Clear Recent Project List
This command clears the Visual Studio recent project list. The Clear Recent Project List command brings up a Clear File dialog which allows any or all recent projects to be selected.
Transform Templates
This command executes a custom tool with associated text templates items. It can be executed from a DSL project node or a DSL folder node.
Close All
This command closes all documents. It can be executed from a document tab”

Posted in Visual Studio | Leave a comment

MVP 2010 Award!

I received great news from Microsoft last week saying that I have been awarded a MVP 2010 Award!

I’d like to thank all those involved in my nomination and look forward to another successful year working with the MVP family.

Mike

Posted in Uncategorized | Leave a comment

WMWifiRouter

I recently went on a trip where another delegate showed off his flashy HTC Touch HD2 acting as a WIFI router. It’s a really neat feature and I was able to use it with my iPod Touch. I added that feature to my ‘must haves’ for my next phone, however I’m pleased to say that I can now do the same with my existing HTC Touch HD. All it takes is WMWifiRouter.

There is a free trial to use and if it works for you it’s just a handful of euro to buy.

Mike

Disclosure: I was provided with a free licence of WMWifiRouter. This does not affect my right to not recommend the product. I find it useful and hopefully you do to.

Posted in Stuff I Use | Leave a comment

Debugging Outlook 2010 Add-ins with Visual Studio 2010

I’m using the Office 2010 RC and VS 2010 RC1.

There is an odd change in behaviour when trying to step into an Outlook 2010 Add-in from VS2010. Using VS2008, I set Outlook as the external program to start, set breakpoints and hit F5. The breakpoints were hit as expected. I upgraded the project to VS2010 but when trying to debug, Outlook started without hitting the breakpoints. I could see the Add-in loading and it was visible after Outlook started.

I was lucky enough to get a tip from a colleague that if I set the Start Action to Start project rather then Start external program, then it worked fine. We’re still trying to determine if this is an intended change in behaviour or a bug. I’d call it a bug, but this is my first attempt at writing an Office Add-in, so I’ve not been on any aliases etc to see if this was ever mentioned.

This may affect other Office applications, but I’ve not had a chance to investigate it further.

[UPDATE: Answers from Misha here: http://blogs.msdn.com/mshneer/archive/2010/03/19/com-shim-wizards-for-vs-2010.aspx]

Mike

Posted in Visual Studio | 2 Comments

The MSBuild Team are Blogging

I met recently with the MSBuild team and was happy to hear that they now have a blogging schedule. From a team that has been fairly quiet on the blogging front for a long time, we now have a steady flow of great posts to read,

 

Keep an eye on the Visual Studio blog for more…

Mike

Posted in MSBuild | Leave a comment

Upgrading Code — Tackling Code Analysis in Visual Studio 2010 (Part 2)

Read Part 1 first. Please note that this is all base on a non-RTM build of Visual Studio 2010, so you may get different mileage out of the final product.

 

As mentioned in Part 1, the ‘CA1062: Validate arguments of public methods’ and ‘CA1305: Specify IFormatProvider’ errors are easy enough, let’s not spend any words on them, so we are left with 79 errors falling into the following three new rules:

  • CA2000: Dispose objects before losing scope (a new rule 62)
  • CA2100: Review SQL queries for security vulnerabilities (a new rule 2)
  • CA2202: Do not dispose objects multiple times (a new rule 15)

CA2000, which although a new rule, illustrates a basic best coding practice. We’ve been caught red handed, guilty, but it’s fantastic that we can now rely directly on Visual Studio to keep a helpful eye on this for us.

If you are new to .net read this, it basically means that you should write something like this

PerformanceCounter pc = new PerformanceCounter(this.CategoryName, this.CounterName, null, this.MachineName);
this.Value = pc.NextValue().ToString(CultureInfo.CurrentCulture);

like this instead

using (PerformanceCounter pc = new PerformanceCounter(this.CategoryName, this.CounterName, null, this.MachineName))
{
    this.Value = pc.NextValue().ToString(CultureInfo.CurrentCulture);
}

That’s an easy fix we just need to do around 60 times, but looking more closely at the code, there are violations where using statements are being used and writing this

DataTable dt = new DataTable { Locale = CultureInfo.CurrentCulture };
this.total = DecimalToSglDbl(Convert.ToDecimal(dt.Compute(this.Expression, string.Empty).ToString(), CultureInfo.CurrentCulture));

like this

using (DataTable dt = new DataTable { Locale = CultureInfo.CurrentCulture })
{
    this.total = DecimalToSglDbl(Convert.ToDecimal(dt.Compute(this.Expression, string.Empty).ToString(), CultureInfo.CurrentCulture));
}

does not resolve the violation. All occurrences of this seemed to have one thing in common. We are using object initializers. I managed to find a Connect issue in which the Code Analysis team advise us to set the properties within the using statement. So the above would become

using (DataTable dt = new DataTable())
{
    dt.Locale = CultureInfo.CurrentCulture;
    this.total = DecimalToSglDbl(Convert.ToDecimal(dt.Compute(this.Expression, string.Empty).ToString(), CultureInfo.CurrentCulture));
}

This is a bit irritating as using object initialization usually makes code easier to read.

We have two violations for CA2100: Review SQL queries for security vulnerabilities. For suppression, this rule says “It is safe to suppress a warning from this rule if the command text does not contain any user input.”. This task is open by design though to take absolutely anything and execute it. It’s in the hands of the user to control how it is used. There may be a case for a version of the task that can handle strict usage of parameters, so that will be added to the backlog.

CA2202 will typically fire on two occasions. Firstly, in code were you have multiple using statements where the outer using objects are used in the inner using constructors, instead of writing this

using (Stream requestStream = request.GetRequestStream())
using (StreamWriter writer = new StreamWriter(requestStream))
{
    writer.Write(post);
}

we should write

Stream requestStream = request.GetRequestStream();
using (StreamWriter writer = new StreamWriter(requestStream))
{
    writer.Write(post);
}

Secondly if you are calling object.Close() within a using statement, instead of writing this

using (FileStream fs = this.versionFile.Create())
{
    fs.Close();
}

we should write

using (FileStream fs = this.versionFile.Create())
{
}

It’s arguable whether you will or have ever seen Dispose throw on ‘ObjectDisposedExeption’, but I don’t see any harm in following the rule.

I wasn’t expecting such on long journey after upgrading, but after going through all the exceptions I think its been a very useful exercise and my confidence in Visual Studio as a whole has been increased. Good luck on your upgrades.

Mike

Posted in Visual Studio | Leave a comment