Alternating Row Styles in Silverlight

Update: There was an issue when using ItemsControl because of the way I was determining if one control was in another (I was using the DataContext.) This example code and attached ZIP have been updated. Because the ItemsControl in Silverlight 4 doesn’t support alternating styles on the child items, I’ve made this set of attached properties to allow you to switch the style on any item contained within an ItemsControl or anything that inherits from it (e.g. ListBox.) I’ve uploaded a sample project, but here’s the entirety of the code for those who don’t want to download...
read more

Dealing with infinite recursion redux

In a previous post about infinite recursion I proposed the use of the ThreadStaticAttribute to deal with infinite recursion. This approach is fine so long as your recursion doesn’t cross threads, if it does the static variable will be reset for each new thread and offer not benefit. Here’s an example, in .NET 4.0, to show you what I mean. In this example I have a simple logging setup, with a list of IListeners which handle messages that a logged with the LogMessage function. It’ll get into a infinite recursive loop becuase the LogToDiskListener attempts to log a message, and the...
read more

Phil’s 10 Rules for the treatment of Exceptions

You know what I love, Exceptions. Exceptions are awesome. They’re an awesome tool that simplify post-mortem debugging, help developers code against and understand the internal workings of an API and they help prevent applications getting into a corrupt state. Even though exceptions are so invaluable, a lot of the time they’re poorly understood, abused, ignored and forgotten. I hope that I can earn some respect for the venerable exception, by setting down some rules on how they should be treated. Phil’s 10 rules for the treatment of Exceptions: Throw early, throw often[1] This is key...
read more

Anonymous types that look the same, are the same

Anonymous types are one of the features that was introduced as part of .net 3.5, they allow a programmer to make a new type dynamically, with its properties defined from its usage. An quick example of an anonymous type might be: var anon = new { AnInteger = 1, AString = "string" }; Console.WriteLine(anon.AString); Behind the scenes, the compiler will create a class that looks somewhat like this: class Anon { public int AnInteger { get; private set; } public string AString { get; private set; } } The type doesn’t look exactly like that, but from a usage point of view,...
read more

Locking on Value Types

Locking is a core principle when it comes to multi-threaded applications, it allows a program make sure a block of code is executed by only one thread at at time. The lock statement in C#, or SyncLock in VB.net, is a really useful command to quickly create a lock and guarantee that the lock is released when the code finishes, even if it throws an exception. private static object _lockObject = new object(); private void SomeFunction() { lock(_lockObject) { // Do something } } There’s one thing that’ll trip up people new to threading, which is that the .net runtime...
read more

More thoughts on Lazy

Something that I missed when I mentioned my thoughts on an implementation of Lazy<T>, was that Microsoft was already a couple of steps ahead of me, and will include their own version in .NET 4.0, the MSDN documentation is here. Their approach is slightly different to mine. Something I tried to do with my implementation was allow you to treat the Lazy<T> as the same type as the generic parameter, by adding the two implicit conversion operators and overriding the equals and hash methods. Microsoft has gone for a slightly different approach by making it immutable, and requiring you call the...
read more

« Previous Entries Next Entries »