Tuesday, July 28, 2009

How to get Property.Name

In some scenarios you might be interested to know current method / property name.
Eg I called some common code from each property and wanted to know calling property name.
Obviously C# doesnt have something like CurrentProperty.Name

You can archive that analysing Stack trace:

You might want to change index no in GetFrame to get interested information


Friday, April 10, 2009

Disable constraints

I've looked the way to temporarily turn off all DB's constraints (eg table relationships)

I needed to copy (using INSERTs) one DBs tables to another DB I know I can archive that by executing commands in proper order (to do not brake relationships)

But it would be easier if I could turn off checking constraints temporarily and turn on it back after operation finish.

So, you can disable FK and CHECK constraints only in SQL 2005+ using below command




Note: Primary keys and unique constraints can not be disabled.


Tuesday, March 10, 2009

Unit testing in Visual Studio 2008 Team System - part IV

Next (maybe not last) part re units tests inside VS.


Useful if one of test methods failed – you can use [Ignore()] attribute to decide later what to do with that test

So, our complete class code looks now as follow:


If we look at above code we can see lot of code duplication – refactoring is necessary.

We can appoint common block of code, it is worth to split tests on small 1 function methods.

To achieve that we will:

  • Define local members (source + destination)
  • Add Init() method marked by attribute [TestInitialize()] – executed before each tests
    (please note method need to be public)
  • Break down test methods on 3 atomic methods

After all our code shall look as follow:


Unit testing in Visual Studio 2008 Team System - part III

In previous post Unit testing in Visual Studio 2008 Team System - part II I’ve described how to perform units tests using new test functionality in VS2008 TS.

In this part I will continue describing test functionality (base on nUnit guide’s example)

In Part II I’ve showed how to use Assert methods to evaluate tests.

Asert() is not only one way to perform tests. Units testing gives also possibility to examine if expected exception occurred.

Let’s return to nUnit example.


If we assume that minimum acceptable account’s balance can be 10F, and if balance is less custom exception (named “InsufficientFundsException“) will be throw.

In that case we can add attribute to our test method informing that we expecting exception:

So, we need to archive this is

1) implement own Exception class:

public class InsufficientFundsException : ApplicationException {}

2) correct orrect our test method:

3) correct our production Account.TransferFunds() method:


Unit testing in Visual Studio 2008 Team System - part II

In previous post (http://csharpmatters.blogspot.com/2009/03/unit-testing-in-visual-studio-2008-team.html) I’ve described new test functionality in VS2008 TS.

In this post I drill down unit tests inside VS2008 more detail.

VS built-in units tests use attributes (similar to NUnit).

Attributes are used to denote various test methods and test classes.

Common attributes:

[TestClass()] mark class representing a unit test case. Class need to be public.

[TestMethod()]marks Test Method

Another popular attributes:

[ClassInitialize()] –code need to be run before running the first test in the class

[ClassCleanup()] – code need to be run after all tests in class

[TestInitialize()] – code need to be run before each test

[TestCleanup ()] – code need to be run after each test


public class MyClassTest

public void MyClassInitTest()

VS provides Assert class with static methods to check various conditions.

All Asserts methods accept 2 parameters: Boolean condition and exception message provided by user. If condition is FALSE: test exception with custom msg is thrown.

More about assert methods here: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert_members.aspx

If you worked with nUnit tool you can see a lot of similarities.

(Please note UI testing is not supported in the current version of unit testing with VS.NET 2008)

Let’s follow nUnit example (http://www.nunit.org/index.php?p=quickStart&r=2.4.7) but using VS built-in tests this time.

Production code example:

Test Class code:

After test run you will see that test failed with message:

TransferFundsTest UnitTests Assert.AreEqual failed. Expected:<250>. Actual:<150>.

All because production method TransferFunds() has no implementation yet.

Let’s correct that as follows:

public void TransferFunds ( Account destination, float amount ) {

destination.Deposit( amount );



After that correction test is passed.

Please find more about testing in next part(s) of post


Unit testing in Visual Studio 2008 Team System

As you probably noticed, VS.NET 2008 comes with unit testing integrated within.

There two ways to create test:

1) From existing, production code

2) Manually

Obviously option 1 requires less work, and I’m going to describe this scenario.

Quick start:

  1. Open your existing project
  2. Right click on the source and select “Create Unit Tests”
  3. Select methods you want to create tests
  4. (Optionally) Change settings (by clicking on “Settings..” button
  5. Click OK to create new test project

After all new test project will be created and added to your solution.

Now you can change run your tests (Menu Test \ Run)

TIP: you can double click on test result record to see more detail view.

What's also very useful: you can also add some brakpoints and start debuging test classes.

Of course auto generated tests are not very valuable there are inconclusive (at least)

You need to change test methods according to unit tests art – but most of dirty work was done by VS!

In next posts I will try explain more about unit testing.

Keep reading!


Wednesday, February 18, 2009

Five favourite new features in VS 2008

1. Extension methods

This feature I like most.
Thanks that you can extend standard object methods

Lets look below example:

As you noticed only difference is “this” word near parameter.
Here you are how to use this:

Please also note that if you add extension to object type – all variable types will get access to this extension!

My favourite example (coming from Scott Gu’s blog http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx )

2. Object Initializers

This is feature I waited long. It gives you possibility to assign in one line public properties in braces after constructor, like this:

3. Automatic Properties

If you don’t like all extra effort you need to do to write fields and properties code you can use below snippet.

CLR 3.x (during compilation) will generate fields and property code for you.
Note: you will not be able to access generated field (only way is only through setters and getters)

4. Local variable type inference

This feature allows you define local variables without defining their types (similar to variant variables in some cases), so you can do this:

Compiler generates appropriate int, string and “SomeType” variables in early-bound strongly-typed fashion.

Personally, I’d prefer explicit types definitions – this gives more clarity in code.
But in some cases (mainly for shorten long code) I’m finding it usefull:

5. Lambdas

Described soon...

Other nice features:
- partial methods