Pages

Monday, May 30, 2011

Code Verification During Development

This post is not for people who use TDD or use unit test.

Here, I am going to talk about a simple idea that lots of you may know about it but aren't using it. And that is 'Immediate Window'.

Immediate window is not just about running simple expressions like '2+2', the subtle point is that you can refer to your objects in your codes.

Imagine you are going to develop a simple method to get all the possible substrings of a text:

public static List<string> GetAllSubTexts(string name)
{
    var result = new List<string>();
    var characters = name.ToCharArray();
    for (int from = 0 ; from < characters.Length - 1 ; from++) {
        for (int to = from + 1 ; to <= characters.Length ; to++) {
            var currentTarget = name.Substring(from , to - from);
            result.Add(currentTarget);
        }
    }

    return result;
}
The above codes seems to work properly. So you may follow your coding without testing and forget this method. One reason developers usually do not do the testing is that it takes time and sometimes it is hard to reach an exact point in the code. But the method who uses it doesn't work properly, and you cant find the cause easily. because you have passed it a while ago.

Simply by calling the method in Immediate Window just after writing it, you probably had found the problem:
MyNamespace.MyClass.GetAllSubTexts("Sam")
Count = 5
    [0]: "S"
    [1]: "Sa"
    [2]: "Sam"
    [3]: "a"
    [4]: "am"
You see, the 'm' is not included. So if you change the outter loop to continue to last index, it is solved.

By using TDD and unit testing practices you may find yourself wasting your time writing unit tests. but at least you can manually unit test your individual methods, specially those who contain complicated algorithms, before they cause problems.

Saturday, March 12, 2011

How to force visual studio to put content into designer file

Web development model in Visual Studio .Net has changed a lot from its first release. At the moment we have different project types for web development.
Recently I have begun working with a project initially developed many years ago and this project has an old structure. So I tried to convert it to a web project. One problem I faced was not having a seperate file for the designer generated codes, because the support for partial classes added later. It is not actually a problem but I wanted to know how I could easily force the visual studio to put designer generated stuff into a seperate file. After some struggling I found this simple method:
  • Just add a file with the name yourfilename.aspx.designer.cs to project.
  • Delete the auto generated codes in the main codes file.
  • Mark your page's class as partial.
  • Then when you save yourfilename.aspx the next time, visual studio will put the generated content into desginger file.

Friday, January 7, 2011

Entity Framework Designer Notes

To zoom : hold down Ctrl while rotating mouse wheels

To scroll horizontally : hold down Shift while while rotating mouse wheels

Friday, November 13, 2009

Set default values of new generated rows at implementing filter

This is a simple trick to handle a common problem

Problem:
You provided a filter option for showing a list, you wanted user to add new items to list, and if list is filtered the new item should be added to the current filtered list.
Requirements:
1. You used the ICollectionView to bind the list, if you don’t do.
2. On of these
  • There is no item that doesn’t belong to any filter.
  • You have a way to recognize newly created objects.
Solution:

First see the below code, pay attention to comments

anICollectionView.Filter = o=>
    {
        var unit = o as Unit;
        if (unit.UnitCategory == null)
        {
            // This is a tricky code, there is 3 points: // 1. This code will be executed only if the list is shown in categorized mode // 2. All units have a unit category, so I didn't change any other unit's unit category // 3. Regarding to point 2 this unit which has no unit category is a new added unit, you bet! unit.UnitCategory = currentUnitCategoryOnFilter; // 4. So I can set the unit category of this newly created unit to the current filter }
        return unit.UnitCategory = currentUnitCategoryOnFilter);
    };

the line
if (unit.UnitCategory == null)
is the way that I recognize this a new object because all units have a unit category.