C#

Today I ran into this problem for the second (and hopefully last) time. Let’s take a look at the words “sorted” and “list”. “sorted” suggests that the items of the list will somehow be sorted. So far, so good. “list” would suggest that you can put any item in the list. Specifically, it isn’t called “SortedSet”, so you could expect that you in fact can add the same item twice with the same key.

Well… that’s where you’d be wrong. The documentation says that the sorted list throws an ArgumentException if “An element with the same key already exists in the SortedList”. Unfortunately I don’t go around looking through the documentation to search for stuff like that. And in fact: you shouldn’t have to. A list is a list and not a set. And if you decide to implement a sorted list with a dictionary as the backing store, then you are – quite frankly – an idiot.

If you would like to save yourself a lot of grief, just don’t use the sorted list. Use a regular List and the Sort method. But be aware, that the Sort method uses the quicksort algorithm which is unstable (elements that are “equal” don’t keep their relative positions to one another).

Sometimes you want to catch and specially handle an SQL-Timeout.
To do this, just put a try-catch around your SQL-Call and catch the SqlException. Now check the Property Number if it is -2 (which stands for Timeout). That’s it.

Here’s a little Code Example how that could look like:

try
{
	RunSomeSqlQuery();
}
catch (SqlException ex)
{
	// Check if it's a TimeoutException
	if (ex.Number == -2)
	{
		// It is...
		// Do whatever you need
	}
	else
	{
		// Other Type of Exception, re-throw it or whatever
		throw;
	}
}

If you want to simulate a Timeout in your SQL-Statement, just use the WAITFOR DELAY SQL-Statement to block for some Time and therefore results in a Timeout.

Here’s an example to wait for 1 Minute:

WAITFOR DELAY '00:01:00'

I like XML. I like it a lot. For most smaller applications a database – even SQLite and the likes – is complete overkill. Still, you have to save your application data somewhere. Enter XML.

I also like to access my application data in a typesafe manner and I don’t like to reinvent the wheel (or in this case, the XML serialization). Enter System.Xml.Serialization. Continue reading System.Xml.Serialization