Showing posts with label Visual Studio .NET 2008. Show all posts
Showing posts with label Visual Studio .NET 2008. Show all posts

Thursday, September 22, 2011

Removing the dreaded #regions using Visual Studio

Back when .NET was released in 2001, the #region/#endregion feature seemed like a good idea at the time. I think it was more a novelty than anything.

If you are a seasoned .NET developer you will probably know what #regions are and how annoying they can be (or you might support them, shame). For those who don't know what they are, this MSDN page might help explain #regions a little: http://msdn.microsoft.com/en-us/library/9a1ybwek.aspx

There have been many posts about #regions over the years. There was one I was reading recently that claimed #regions are a sign of code smell! That is pushing it a bit, they are not code smells but are very annoying and if I used #regions it might say something about my code. It might say that I write large bloat god type classes that need regions to split it up in order to maintain it more easily - that you could argue could *potentially* lead to a code smell.

When I open some source code file, often it will not be my code or sometimes it might be. Lets assume its not. Lets also assume that the developer of that source code like regions. The first thing I get is the screen shot below:


Figure 1: God class with regions and outlining feature turned on


So now in order to understand this GodClass, I'm going to have to expand all those regions as I didn't write this code. All I want to do is get at the code to understand it, these #regions are preventing, or rather, making it more difficult for me from doing so.

The title to this post reads "Removing the dreaded #regions using Visual Studio". So if you're in a shop that loves to use #regions or your code base is such that to remove all the #regions would be weeks of work, fear not! I didn't know this feature existed in Visual Studio up until a couple of days ago but you can actually turn off the *outlining feature* that makes #regions possible.

To turn off this feature do the following:

  1. In Visual Studio click Tools -> Options
  2. In the options tree view, click Text Editor -> C# -> Advanced
  3. Then untick option Enter outlining mode when files open (see figure 2 below for an example)
  4. Click OK
  5. Enjoy!

Figure 2: Turning off #region support in Visual Studio

As far as I am aware there doesn't seem to be support for this if you're using VB.NET. This only seems to be a C# feature right now.

So now if I load the GodClass in Visual Studio, I get the following source code:


Figure 3: Outlining turned off

It's not perfect I admit as the regions are still there, but at least I can now see the code without having multiple mouse clicks in doing so...

Wednesday, May 12, 2010

MSTest: Not Executed after aborting a debugged MSTest

If you try to debug a MS Test unit test in VS 2008 but stop the debugger before the test is completed (abort the test) the test status will be marked with "Aborted". Which is fine and expected. But if you then try to execute any other test, whether this is debugging a test or just executing a test, it will fail and the status of all attempted tests will be "Not Executed". And from here in no tests will ever execute again..until you kill process "vsperfmon.exe". If you kill that process, then unit tests will then continue to execute.

This defect seems to be fixed in VS 2010 which is great but it still exists in VS 2008 which is a pain as Windows Mobile developers still require VS 2008 to develop WM apps (this defect applies to both desktop and device tests).

There is a bug report on Microsoft Connect: https://connect.microsoft.com/VisualStudio/feedback/details/299925/after-debugging-a-unit-test-vsperfmon-exe-must-be-killed-to-be-able-run-further-tests

As always, if you find this defect annoying, then use the site above to vote for a fix.

This bug has been around for sometime and it's only today I learned the workaround!

Sunday, January 18, 2009

No Visual Studio 2008 templates after installing Enterprise Library 4.1

I installed the Enterprise Library 4.1 successfully today and after doing so I attempted to create a project in Visual Studio 2008 and then encountered this warning message:



Then when I loaded the new project dialog in VS, all project templates were missing.

A simple solution to this problem is closing down all instances of Visual Studio then run this on the command-line: devenv /installvstemplates

Saturday, November 22, 2008

Extension method support in Compact Framework 2.0 applications

I had a discussion recently with my fellow MVPs and you can use C# 3.0 language features in a CF 2.0 app with VS 2008 and even extension methods with a small hack.

Simply create an ExtensionAttribute class:
using System.Runtime.CompilerServices
{
public class ExtensionAttribute : Attribute{}
}
Then you need to declare this attribute on each extension method in order to implement extension methods. This is because extension methods require the new ExtensionAttribute class introduced in System.Core.dll in .NET 3.5.

Daniel Moth has talked about this in depth here.

Sunday, September 14, 2008

Tuesday, July 08, 2008

Microsoft Visual Studio SP1 (Beta) does not apply, or is blocked by another...

I mentioned recently the availability of Visual Studio 2008 SP1 Beta here.

If you try to install this you might get the following error if Silverlight Tools Beta 1 is installed on your machine:

"Microsoft Visual Studio 2008 SP1 (Beta) does not apply, or is blocked by another condition on your system. Please click the link below for more details".



To fix this, simply uninstall the Silverlight Tools Beta 1 package.

See the release notes for this SP here.

When you do finally get this installed, one of the upgrade features of VS 2008 is the ADO.NET Entity Framework for LINQ to Entity support - very cool. I will be talking about this soon...


Wednesday, March 19, 2008

XSLT Profiler for Visual Studio 2008 CTP

I wrote a blog recently regarding a Biztalk XSLT mapper tool named Xselerator which is a tool that allows you to debug XSLT and run translations. I mentioned at the time Visual Studio didn't support this, but now it does with this plug-in, but only on VS 2008 Team Suite with the performance tools feature installed.

Get it from here: http://www.microsoft.com/downloads/details.aspx?FamilyId=F43314ED-95B7-435F-95C5-0E326E64543B&displaylang=en

Monday, March 17, 2008

Rolling back Visual Studio 2008 projects to Visual Studio 2005

You might ask, why would I want to roll back my VS 2008 projects to VS 2005? Well if like me you were hasty in upgrading your solution and didn't relise or forgot that CF 1.0 projects are not supported under VS 2008 and upgrading will upgrade CF 1.0 to CF 2.0, then you have every reason to do so.

We still have CF 1.0 projects or project which is used for an autorun app which gets shipped on a memory card on PPC 2003 SE and later because CF 1.0 is installed in ROM from PPC 2003 SE. Doing this enables our application to self-install on cold-boots or power failures.

Luckily it isn't too difficult rolling back your projects (although you should use some kind of source control system to roll back to). If you don't a source control system or just curious how to do it, it is dead easy.

Simply open up the project and change the ToolsVersion attribute to 2.0. Change the TargetFrameworkVersion element to 2.0. You can get rid of the Import element which specifies CF 3.5. IE:
<import condition="'$(TargetFrameworkVersion)' == 'v3.5'"
project="$(MSBuildBinPath)\Microsoft.CompactFramework.CSharp.targets"></import>

Ensure a CF 1.0 or CF 2.0 exists. If you have a desktop project you might have a
<requiredtargetframework>3.5</requiredtargetframework>
for some of the framework assemblies, if rolling back, these can be deleted.

Rolling back the solution couldn't be easier. At the header of your solution file (.sln) you'll have the following:

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008

Change the above to:

Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005

That's it!

Friday, March 14, 2008

Device Unit Testing using Visual Studio 2008 Team System

With the release of Visual Studio 2008 device unit testing or in fact desktop unit testing is now built into the IDE which makes unit testing alot easier than before. Note device unit testing is only supported on Visual Studio Team System 2008 with Test Edition or Team Suite. See here for a Visual Studio product comparison.

If you would like to learn more about what agile unit testing is see here.

With Visual Studio 2005 there were tools such as Test Driven.net, NUnit, MBUnit typically were used and still are today, in fact we still use NUnit mainly because we like it, it works and we have hundreds of tests written using it. There are differences with the above tools, we chose NUnit because it was ported from JUnit which worked also.

This article will cover how to use unit testing on a Windows Mobile device and show how simple it is. In fact I am very fond of it as it does in fact do alot of work for you that NUnit doesn't. Also the difference with the VS unit testing over NUnit is that in VS the unit testing is integrated in the IDE.

There are two ways to create unit tests in VS 2008, one is to create tests from production code or to create them by hand. Of course it is less work to create the tests from production code so this is what I'll talk about in this article.

1. Start off with a simple device class library. In this example I have created a simple calculator which adds two numbers together. The code looks like the following:
namespace Calculator
{
public class SimpleCalculator
{
public decimal Add(decimal num1, decimal num2)
{
return num1 + num2;
}
}
}
As you can see, it is very simple.

2. Right click the source for which you would like to create the test project, then select "Create Unit Tests..." option.



3. Select the method for which you would like to create the test.



4. Clicking on "Settings..." button will allow you to configure how the unit test wizard will create your test.

Most of the options are self explanatory. I always change the name to the name of the class coupled with "Fixture" ie in this case "SimpleCalculatorFixture". It stems from NUnit days.

The only option that needs to be clarified below is the "Mark all test results Inconclusive by default". This option forces the test to fail when run because it is not implemented. Of course it is good practice to honor this setting.



5. Clicking OK to the Test Generation Settings dialog then click OK again on the Create Unit Tests dialog will prompt you to enter a name for your new test project, enter a name and click OK.

6. After completing the above Visual Studio creates the test project and some test methods. quite neat when you think in the past you'd have to do all this yourself.

In the above example the test code VS generated looks like the following:
using Calculator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace CalculatorTest
{

[TestClass()]
public class SimpleCalculatorTest
{
private TestContext testContextInstance;

public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}

#region Additional test attributes
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion


///
///A test for Add
///

[TestMethod()]
public void AddTest()
{
SimpleCalculator target = new SimpleCalculator();
Decimal num1 = new Decimal();
Decimal num2 = new Decimal();
Decimal expected = new Decimal();
Decimal actual;
actual = target.Add(num1, num2);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
}
}

As you can see from the above test code, it is very similar to how things are done using NUnit. Notice the similar attributes used to mark methods. The most notable one being the TestClass attribute the NUnit equivilent is TestFixture. These attributes and all testing methods etc can be found in a simple assembly named: Microsoft.VisualStudio.TestTools.UnitTesting.dll. You don't need to add this to your test project because Visual Studio has already done this for you. It doesn't get any easier than this!!

Many of the methods VS has added for us such as MyClassInitialize, MyClassCleanup etc are commented out they have been added for ease of implementation if required.

7. Now we have everything in place, lets see what happens when we run the test, bearing in mind we haven't written any tests yet, we have simply let VS do its job and create a test project for us.
/// 
///A test for Add
///

[TestMethod()]
public void AddTest()
{
SimpleCalculator target = new SimpleCalculator();
Decimal num1 = new Decimal();
Decimal num2 = new Decimal();
Decimal expected = new Decimal();
Decimal actual;
actual = target.Add(num1, num2);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
Now running the test will execute the above, of course the line Assert.AreEqual will fail because we have already written our production code which adds the two numbers together so the result of this method call will not be equal. Visual Studio has guessed here what the likely result might be, clever but not quite clever enough!

So running the above test results in failure on my machine. I am attemping to run the test on an emulator. The error message I am getting is:

The test adapter ('Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') required to execute this test could not be loaded. Check that the test adapter is installed properly. Exception of type 'Microsoft.VisualStudio.SmartDevice.TestHostAdapter.DeviceAgent.NetCFNotInstalledException' was thrown.
I wrote a blog regarding this error a while back here.

This basically means .NET CF 3.5 is not installed and VS doesn't deploy .NET CF 3.5 before executing the test if required even though the option under the test project properties Devices tab "Deploy the latest version of the .NET Compact Framework (including service packs)" is set.




You can simply deploy a Windows Forms application with the above option set and VS will deploy CF 3.5 just as it's always done, if you don't have a Windows Forms application, deploy the CAB file to the device/emulator manually then run it. After this the tests should execute.

8. Now I have fixed the above problem, lets try running the test again. This time I get the following results in the new Test Results window.



From the above Test Results window it is difficult to read the error as to why the test failed. Double clicking the error row loads the properties window for this particular test which gives us some more information as to why the test failed.




9. As we can see from the error message above the Asset.Inconclusive failed. If you remember when VS creates a test template, it inserts the Assert.Inconclusive which will fail when run. So to fix this problem, we simply change the test to the following:
        /// 
///A test for Add
///

[TestMethod()]
public void AddTest()
{
SimpleCalculator target = new SimpleCalculator();
Decimal num1 = new Decimal();
Decimal num2 = new Decimal();
Decimal expected = new Decimal();
Decimal actual;
actual = target.Add(num1, num2);
Assert.AreEqual(expected, actual);
}
We have simply removed the call to the static method Inconclusive.

Now if we run the above test we get the following:



Suprisingly, the test passes. It's not really that suprising because if you look at the AddTest method above, you'll notice that VS has created an instance of two decimal objects which both will be 0 and declared a returning type which isn't instanciated, but the AddTest method creates it and passes it back to the test method. So we are performing this calculation: 0 + 0. Of course the result will be 0 which is why the test passes.

Explore the Assert class, there are many methods to use against your tests much like NUnit. It seems the Testing framework in VS 2008 was built on NUnit and I like it alot!

Wednesday, February 13, 2008

Upgrading your device solutions to Visual Studio 2008 and the .NET CF 3.5

As the title says upgrading your pre 2008 device solutions to Visual Studio 2008 and to run under the .NET CF 3.5 is a two step process. This is because Visual Studio 2008 supports multi-targeting.

By default when you upgrade your VS 2005 projects to VS 2008 they continue to target the .NET Framework 2.0. This is true for both desktop and devices. This article quickly talks about how you target the new frameworks after conversion.

1. Open your VS 2005 solution in VS 2008. The conversion wizard will load, simply run though this.



2. This particular solution contains device projects as well as desktop projects.

3. As you can see, after the project has been converted, it is still compiled against the CF 2.0.



4. In VS 2008 there is a new menu option labeled "Upgrade Project" (among others) under Project or the same option can be accessed under the context menu by right clicking the project.

NOTE: This new option is only visible to device applications.

Clicking it will upgrade your Compact Framework 2.0 application to compile against the Compact Framework 3.5.



5. Clicking the "Upgrade Project" menu option will prompt you to continue.



Once you have clicked Yes, the process is very quick and does indeed upgrade your device application to compile against the CF 3.5.



6. After doing the above you will notice the "Upgrade Project" menu option will disapear from the project context and main menus.



Although this post is mainly about devices I thought I'd mention converting your desktop applications from .NET Framework 2.0 to .NET Framework 3.5 is slightly different. There is no "Upgrade Project" menu option for desktop projects, instead you have to use the muli-targeting support features under project properties to target the .NET Framework 3.5.

Sunday, February 03, 2008

NETCFv35.Messages is the new System_SR

In Visual Studio 2008 and the .NET Compact Framework 3.5, NETCF35.Messages replaces the System_SR found in previous versions of the CF.

So if you had the message similar to the following running pre CF 3.5 when a framework exception occured: "An error message cannot be displayed because an optional resource assembly containing it cannot be found". A solution to this was to install System_SR_ENU_wm.cab (if running Windows Mobile) or System_SR_ENU.cab (if running Windows CE). Both of which ships with the Windows Mobile SDK's. Doing this only worked if you were lucky! not really, there is some science to it, science that I am not sure of. A combination of installing the CF and combination of localized apps on the device

In Visual Studio 2008, this new cab that replaces System_SR is named: (NETCFv35.Messages.ENU.wm.cab for Windows Mobile or NETCFv35.Messages.ENU.cab for Windows CE) is not installed by VS initially when you start debugging whereas in pre VS 2008 we enjoyed the automatic install on debugging for the first time. You have to install this CAB explicitly. This cab can be found here: C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\Diagnostics - of course assuming C: is where you decided to install the CF.

Sunday, January 27, 2008

Developer UK Launch –Visual Studio 2008, SQL Server 2008, Windows Server 2008

I have been asked by Microsoft to help out on the Ask the Experts panel for the Developer UK Launch conference held at the ICC in Birmingham on the 19th March 2008 - an all day event.

I will be helping out with the Visual Studio 2008 team, so if you are attending be sure to ask not just mobility questions but any VS related questions.

It seems this event is now full but it is possible to put your name on the waiting list by calling: 0870 166 6640. See here for more info.

Tuesday, December 25, 2007

Microsoft.VisualStudio.SmartDevice. TestHostAdapter.DeviceAgent. NetCFNotInstalledException

You might encounter the following exception when attempting to run a unit test on your device using the new built-in Unit Testing features that come with Visual Studio .NET 2008:

The test adapter ('Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') required to execute this test could not be loaded. Check that the test adapter is installed properly. Exception of type 'Microsoft.VisualStudio.SmartDevice.TestHostAdapter.DeviceAgent.NetCFNotInstalledException' was thrown.

A simple solution to this is to install the .NET CF 3.5 on your device before running the test.

Thursday, November 22, 2007

Where has Windows Mobile 6 SDK gone!!?

After installing Visual Studio 2008 I was unable to target Windows Mobile 6 devices from within the '08 IDE - even though I have Windows Mobile 6 SDK installed and can still compile against WM6 in Visual Studio 2005.

This is a known issue by Microsoft and is a result of installing Visual Studio 2008 and to fix, simply re-install the Windows Mobile 6 SDK.

Wednesday, November 21, 2007

Extracting Visual Studio 2008 UDF from an ISO image

So you've just downloaded Visual Studio 2008 in .iso format from MSDN subscriptions...what now? Well you can burn this image to DVD then install from there. But what if you want to just extract the .iso and install from HD?

To do this you need a UDF (Universal Disk Format) compatible extractor. You can use either ISO buster (not free) or WinRAR which supports UDF from version 3.7 and onwards.

Tuesday, November 20, 2007

Visual Studio 2008 & .NET Framework 3.5 RTM is here!

UPDATE: If you are an MSDN subscriber and have the monthly MSDN shipments, VS 2008 will only be included from Jan 2008 and onwards and not before.

Yesterday (Monday 19th November 2007) the RTM version of Microsoft Visual Studio 2008 was released. You can download this via MSDN subscriptions or a 90 day trial version if you do not have an MSDN subscription.

I am downloading VS 2008 Team Suite edition as I write this blog entry. See here for more information. It is a rather hefty download at just short of 4 GB!

I am in the process of writing a blog entry regarding what's new for device application developers with the advent of VS 2008, so watch this space...