Peter's Software House

‘ngen /delete *’ Restored my sanity

Visual Studio 2008 was crashing when opening an aspx/ascx designer, changes made to aspx/ascx files not reflected in code behind etc…

I was getting really pi$$ed off.  Tried deleting all the source, recreate from Source Control, no luck, close to throwing PC out the window… ;)

Then found a blog post somewhere to try ‘ngen /delete *’, did that and everything started working.

Whooohooo, now I can catch-up on the 4hours of lost work. :(

Posted by Pieter | with no comments
Filed under: ,

Omea 3 Executable and Google Reader

I went over the unpublished comments on my blog a couple of days ago and in between the thousands of spam comments I found a number of comments that I approved and published.  I had quite a few ask me for a link to Omea 3 executables.  So I’ve uploaded the BinRelease folder from my PC and put it on my SkyDrive.  I hope it works for those that try it.  here is the link: http://cid-95226bf8d5337e4d.skydrive.live.com/self.aspx/Public/Omea3%20BinRelease.rar

If it doesn’t work drop me a line (comment here, or send me a message) and I’ll try and help.

I loved using Omea, but I’ve since moved on.  I now use Google Reader and must say it does the job for me.  I can now read my blogs anywhere I want.  No more concerns about moving my subscriptions from PC to PC.

Posted by Pieter | with no comments
Filed under:

Minimizing the Ribbon

The Ribbon Microsoft has introduced in Office 2007 takes up a lot of real estate.  This is especially apparent when you use a laptop with a smaller screen or one of the new very popular netbooks.  Most people it seems though does not realize, but you can minimize the Ribbon.  All you need to do is right click anywhere on the Ribbon and select the ‘Minimize the Ribbon’ options.

image

Now the Ribbon will be minimized and you will have quite a bit of extra space to work with. 

image

If you need to use the Ribbon, simple place your mouse pointer over the minimized Ribbon and it will drop down for you to use.

Posted by Pieter | with no comments
Filed under: ,

Busy playing with .NET Reflector Version 6.0.0.x

image

Go and get it from the EAP over at Red-Gate.

Posted by Pieter | with no comments
Filed under:

Get the Column information for a Table using T-SQL

It is really simple really, in this sample I am getting the column information for a table called ‘Account’:

SELECT     Columns.name AS ColumnName, ColumnTypes.name AS Type, Columns.prec AS Precision, Columns.scale AS Scale, 
                      Columns.isnullable AS IsNullable
FROM         sys.sysobjects AS Tables INNER JOIN
                      sys.syscolumns AS Columns ON Columns.id = Tables.id INNER JOIN
                      sys.systypes AS ColumnTypes ON Columns.xtype = ColumnTypes.xtype
WHERE     (Tables.type = 'U') AND (Tables.name = N'Account')
ORDER BY ColumnName
Posted by Pieter | with no comments
Filed under: ,

Get all the user Tables, Views or Stored Procedures for a Database with their schema using T-SQL

Get all the Tables:

SELECT     sys.schemas.name AS [Schema], sys.sysobjects.name
FROM         sys.sysobjects INNER JOIN
                      sys.schemas ON sys.sysobjects.uid = sys.schemas.schema_id
WHERE     (sys.sysobjects.type = 'U')
ORDER BY sys.sysobjects.name

Get all the Views:

SELECT     sys.schemas.name AS [Schema], sys.sysobjects.name
FROM         sys.sysobjects INNER JOIN
                      sys.schemas ON sys.sysobjects.uid = sys.schemas.schema_id
WHERE     (sys.sysobjects.type = 'V')
ORDER BY sys.sysobjects.name

Get all the Stored Procedures:

SELECT     sys.schemas.name AS [Schema], sys.sysobjects.name
FROM         sys.sysobjects INNER JOIN
                      sys.schemas ON sys.sysobjects.uid = sys.schemas.schema_id
WHERE     (sys.sysobjects.type = 'P')
ORDER BY sys.sysobjects.name
Posted by Pieter | with no comments
Filed under: ,

Set the dictionary to use in Word 2007

In Word 2007 you can pick a different language setting at any time.  This is especially useful if you write in several languages or simply different English dialects. ;)

image

Click on the language bar on the bottom left hand side of the Word status bar.

You will get the following view:

image

Select your language you want to use or make a different language the default by clicking the ‘Default…’ button. 

Posted by Pieter | with no comments
Filed under: ,

Things I learned last week #3

Well actually more like things I learned the last three months as my last ‘Things I learned last week #2’ was on 24th of June.

Things are crazy busy in my life at the moment.  With work going flat out and with study coming to an end in less than 4 weeks, I have no time for blogging.  But I had a few minutes and thought I’d post a quick update.

Now back to working on my paper.

Posted by Pieter | with no comments
Filed under:

Magic Numbers – How to remove them from your Codebase.

I am not a fan Magic Numbers at all.  But most code bases I have seen and/or worked on have them too varying degrees.  Using simple OO techniques can help you avoid them.  In this post I am going to discuss how to avoid Magic Numbers. 

Let’s start by creating a simple class of say a Person.

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public char Status { get; set; }
}

Nothing special, just a firstname, lastname and status.  Let’s write a test to use it (Yea I know, should have written the test first!)

[TestMethod]
public void TestMethod1()
{
    Person p = new Person();
    p.Status = 'C';
    Assert.AreEqual(p.Status, 'C');
}

Ok, this works, but WTF does ‘C’ mean?  Closed? Created? (The test Name doesn’t help)

Let’s make a change to the Test to make it clear:

 

[TestMethod]
public void Person_Status_Closed()
{
    Person p = new Person();
    p.Status = Person.Closed;
    Assert.AreEqual(p.Status, Person.Closed);
}

So we changed the ‘C’ to a constant called Closed, which we have added to the Person class. This is cool for now, when I read this code it is clear that the status I am assigning to the Person class is Closed. A funny status to pick when you have just created the object, so let’s say the default status for a person is Active. Let’s write a test for that first:

[TestMethod]
public void PersonStatus_Default_Active()
{
    Person p = new Person();
    Assert.AreEqual(p.Status, Person.Active);
}

And let’s update the person class:

public class Person
{
    public const char Active = 'A';
    public const char Closed = 'C';

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public char Status { get; set; }

    public Person()
    {
        Status = Active;
    }
}

All going pretty well so far.  But this does not stop us from using Magic Numbers, i.e. I can still check for ‘A’ or ‘C’ or I can assign any char value I like to status.  This is especially likely to happen when you are working on a large codebase and an inexperienced developer has to make a change in code and their first response is to use ‘A’ instead of Person.Active.  Sure this should be picked up during code reviews, but why not make it impossible to do this in the first place?  How?  Very easily, replace the Person.Status with a Status class.

Let’s create the test first:

[TestMethod]
public void PersonStatus_Default_Active()
{
    Person p = new Person();
    Assert.AreEqual(p.Status, PersonStatus.Active);
}

A simple change, can you spot it?

Let’s have a look at the implementation:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public PersonStatus Status { get; set; }

    public Person()
    {
        Status = PersonStatus.Active;
    }
}
public class PersonStatus
{
    public static PersonStatus Active = new PersonStatus('A', "Active");
    public static PersonStatus Closed = new PersonStatus('C', "Closed");

    public char Code { get; set; }
    public string Description { get; set; }

    public PersonStatus(char code, string description)
    {
        Code = code;
        Description = description;
    }
}

A huge improvement, you now have to use a PersonStatus object to indicate the status of Person.  But you can still create your own version PersonStatus, i.e. you can set status = new PersonStatus(‘X’, ‘Invalid Status’) and it will work.  Let’s add some validation. 

Test first:

[TestMethod]
[ExpectedException(typeof(InvalidStatusException))]
public void PersonStatus_InvalidStatus_ExpectException()
{
    Person p = new Person();
    p.Status = new PersonStatus('X', "Invalid Status");
    Assert.Fail("Expected an InvalidStatusException");
}

And the Implementation:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    private PersonStatus _status = PersonStatus.Active;
    public PersonStatus Status 
    {
        get
        {
            return _status;
        }
        set
        {
            if (!PersonStatus.IsValid(value))
                throw new InvalidStatusException(string.Format("'{0}' - {1} is an invalid status", value.Code, value.Description));
            _status = value;
        }
    }
}
public class PersonStatus
{
    public static PersonStatus Active = new PersonStatus('A', "Active");
    public static PersonStatus Closed = new PersonStatus('C', "Closed");

    private static IList<PersonStatus> _validStatusList = new List<PersonStatus>();

    public char Code { get; set; }
    public string Description { get; set; }

    static PersonStatus()
    {
        _validStatusList.Add(Active);
        _validStatusList.Add(Closed);
    }

    public static bool IsValid(PersonStatus status)
    {
        return _validStatusList.Contains(status);
    }

    public PersonStatus(char code, string description)
    {
        Code = code;
        Description = description;
    }
}
[Serializable]
public class InvalidStatusException : Exception
{
    public InvalidStatusException() { }
    public InvalidStatusException(string message) : base(message) { }
    public InvalidStatusException(string message, Exception inner) : base(message, inner) { }
    protected InvalidStatusException(SerializationInfo info, StreamingContext context)
        : base(info, context) { }
}
In my next post I will show how you can use this same technique with an ORM tool.
Posted by Pieter | 4 comment(s)
Filed under: ,

Icon/Cursor Paint.NET Plug-In

I’ve just learned that my #1 image editor (Paint.NET) has a Plug-in that allows you to edit Icons and Cursors – Fantastic!

Get it here: http://www.evanolds.com/pdnicocur.html

Posted by Pieter | 1 comment(s)
Filed under:

Things I learned Today

Well actually it was yesterday, but who cares…

I HATE COM!!!

Yea I had to struggle yesterday with a VB6 application that calls out to a VB.NET app using COM.  The age old problem of ‘but it works on my machine’ and in this case the dev server and the deployment server.   But for some freaking crazy reason it did not want to work on the Production server.  After tearing my hair our for several hours I decided to change the interface and now we are spawning a new process and passing a couple of parameters to a console app using command line parameters and that worked first time and every time since.   Thank God there is more than one way to skin a cat.  It might not be pretty but it works, consistently.

Posted by Pieter | with no comments
Filed under:

Another classic WTF

I ran into this WTF a few minutes ago:

string valueFormat = "$#,##0.00";

// Updating a label
lblDecimalValue.Text = decimalValue.ToString(valueFormat);

// Updating another label, notice anything strange?
lblSomeOtherLabel.Text = (accountBalance + decimal.Parse(lblDecimalValue.Text.Replace("$", string.Empty))).ToString(valueFormat);

What should have happened in Line 7?

lblSomeOtherLabel.Text = (accountBalance + decimalValue).ToString(valueFormat);

Why? Well that is pretty obvious, you already have decimalValue so why would you want to parse the string value to get a number you already have?

Posted by Pieter | with no comments
Filed under:

How to format DateTime using ToString

Another WTF:

// I saw this code today and went WTF!!!!
string oldway = DateTime.Now.Year + "-" +    
DateTime.Now.Month.ToString("00") + "-" + DateTime.Now.Day.ToString("00") +    
    "T" + DateTime.Now.Hour.ToString("00") + ":" + DateTime.Now.Minute.ToString("00") +    
    ":" + DateTime.Now.Second.ToString("00");

// I changed it to this
string newWay = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss");

// And to prove it worked I placed it all in a test and checked that both returns the same result.
Assert.AreEqual(oldway, newWay);
Posted by Pieter | 2 comment(s)
Filed under: ,

Use Null Coalescing Operator

// WTF - Code
// Reads the DB twice if an Address is found
Address addressExisting = personExisting.ReadAddress(Database) == null ? new Address() : personExisting.ReadAddress(Database);

// What should you use?
// The Null Coalescing Operator
Address addressExisting = personExisting.ReadAddress(Database) ?? new Address();
Posted by Pieter | with no comments
Filed under:

You have found a bug – how to find out which process created a window.

There was a bit of a panic today when this came up.  Some thought it was a virus, others thought it was someone playing a trick.

image

 

So I opened up Spy++.  Found the Window.

image

When you right click and select properties.

image

Now select the Process tab and click on the Process ID link.

image

And then you can see which process this window came from:

image

As you can see in this case it was SVN-Monitor.

Posted by Pieter | with no comments
Filed under:
More Posts Next page »