Archive for April, 2007

Visual Studio 2005 nag: “The following breakpoint cannot be set”

Monday, April 30th, 2007

If you start an application in debugging mode under Visual Studio 2005 but then realise that you have forgotten to change some bits of code and quickly kill the app, then VS will greet you with message box about not being able to set breakpoint… for each of the breakpoints in your code and there is no way to skip that damn message so you have to click or press Enter for all those dialogs, grrrrrrrrrrrrr!

Avoid using Array.Sort on string arrays

Thursday, April 26th, 2007

Array.Sort is the first port of call for sorting of data in arrays, however one must be very careful when using it on string arrays: the hidden problem is not just that the default comparison method that Sort will use for strings will take into account locale (culture) and thus make words like say German word “daß” will be deemed equal to “dass”, this might be acceptable behavior under some conditions, however there is a bug in dealing with dashes “-” which appears to be half-acknowledged by Microsoft, but they keep it all very quiet and it is likely that for compatibility reasons they will never fix it.

What this all means is that Array.Sort on a string array may produce sorted results that are valid for a given machine, but the same “sorted” data will be invalid on another machine! This is a terrible situation that can lead to very weird bugs based on assumption that sorted data will remain sorted elsewhere, it won’t! The solution is to avoid using default Array.Sort and either use your own implementation of quick sorting and/or using string.CompareOrdinal to ensure that sorted data stays this way regardless of machine type.

Note that the very same problem applies to usage of string.Compare() – it is this function that causes troubles for default sorting, so be extremely careful when using it, best to avoid it altogether.