How I Finally Discovered when to use Debugger.Break()

The existence of the System.Diagnostics.Debugger class is not something new to me, but I never figured out an usage for it. I never used it, and my knowledge was limited to think that it was a bit useless to code a breakpoint while you could set it in visual studio.

However, this makes quite a lot of sense in some scenarios, like programs that execute too fast, or PowerShell cmdlet that you don’t have a chance to attach to.

Related StackOverflow question: http://stackoverflow.com/questions/1432359/what-are-the-benefits-of-using-system-diagnostics-debugger-break-over-attach-to

Live and learn!

Track Current Item in Visual Studio

Pretty dumb post, but I always forget where this option is, yet I hardly can live without it.

So here it is:

TrackCurrentItemVS

Something else I can’t live without and yet is not set by default is the line numbers. I really don’t understand why this is not set as default!

Recycling Application Pools with PowerShell

A few days ago, a colleague and I were looking for a way to recycle Application Pools in a PowerShell script.

Turned out that we couldn’t find any “native” way of doing this, but it is still possible to do it using the Microsoft.Web.Administration.ServerManager class:

[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
 
function RecycleAppPools()
{
        $serv = new-object Microsoft.Web.Administration.ServerManager
 
        $serv.ApplicationPools | % { $_.Recycle() }
}

Not much, but still pretty useful. However, this will recycle all the Application Pools on the IIS server. In our case, we were only interested in recycling the Sharepoint related pools.

A simple Where did the trick:

$serv.ApplicationPools | ? { $_.Name -like "SharePoint*" } | % { $_.Recycle() }

Reading an Int32 from a DataRow coming from SharePoint

In my previous post, I described how to convert a String to an Int32 making sure that Convert.ToInt32 works all the time for positive integers.

Now, there is still an issue with that method: when the integer is negative. In that case, adding a 0 in front of it does not work and makes the conversion throw a FormatException, as the underlying parse chokes on the “0-”.

Now, the obvious ultimate solution to that problem is simply to write an extension method for DataRow to try to parse the value as an Int32:

internal static int FieldAsInt32(this DataRow dataRow, String columnName, int defaultValue)
{
    var fieldValue = dataRow.Field<String>(columnName);

    int value;

    if (Int32.TryParse(fieldValue, out value))
    {
        return value;
    }
    else
    {
        return defaultValue;
    }

}

internal static int FieldAsInt32(this DataRow dataRow, String columnName)
{
    return dataRow.FieldAsInt32(columnName, 0);
}

I added an overload that takes the default value, i.e. the value that is to be returned if the parse fails. The default for that is 0. This might not be useful in all cases, but in our case this field is used to order elements, so if it can be parsed, it has to return a very high value to make sure it is the latest in the ordered sequence.

Convert.ToInt32(String) With an Empty String

Lately, I have been struggling with the Convert.ToInt32 overload that takes a String as a parameter. Basically, it’s the same as Int32.Parse, except that if the given String is null, it returns 0 instead of throwing a FormatException. That’s quite cool, but Convert.ToInt32 still throws when the argument is an empty String…

Now, my particular case is that I’m retrieving data from Sharepoint, and that the field retrieved can be empty if the user left it blank (when retrieving the field trough a DataTable).

The workaround to that issue is actually pretty silly: just add a 0 at the beginning of the parsed String, and it will work all the time (and return 0 when the string is empty, as it does with null). Now mind you, this only works with positive integers. If your integer is negative, adding a 0 in front of it will make the Convert.ToInt32 throw a FormatException again…

String theInt = "";

//Throws...
Console.WriteLine(Convert.ToInt32(theInt));

//Doesn't throw
Console.WriteLine(Convert.ToInt32("0" + theInt));

theInt = Int32.MaxValue.ToString();

//Doesn't throw
Console.WriteLine(Convert.ToInt32("0" + theInt));

theInt = Int32.MinValue.ToString();

//Throws again...
Console.WriteLine(Convert.ToInt32("0" + theInt));

So, this is safe to use if you are certain that the integer in the String is always positive.

PS: note that I always write String with a capital S in my code (and in text), this is a habit left from Java I guess…

← Previous PageNext Page →