Tips

Doing less with LINQ

While reading a recent article in MSDN magazine, I came across some code that gives me a great opportunity to show how to get more done with less effort by using LINQ.

Avoiding Stack Overflow with Params

Here's a pattern that I've seen occur a number of times - a pair of method overloads, one accepting IEnumerable<T> and the other accepting params T[].

WCF and IList<T>

Here's an interesting problem I encountered recently with a WCF based web service and the humble IList<T> interface.

I'm working on a classic 3-teir system: Client/Application/Database. The Client and Application are using Windows Communication Foundation (WCF) to interact. For convenience, we have an assembly of Data Transfer Objects (or DTOs) which is shared across both the client and the server.

While working on a recent system enhancement, I encountered an odd error - a NotSupportedException with the message "Collection was of a fixed size".

Using TypeConverters with WPF

I'm working on a little project at the moment which is dynamically building a WPF user interface based on a configuration file loaded when the application starts. This involves code to manually create all of the bits and pieces that are normally specified directly in a Xaml file.

One useful shortcut I've found is to leverage .NETs built in TypeConverter framework - the same technique used by the Xaml loader itself.

Using Windows SignOn with PowerBuilder and SQL Server

Following The Good Samaritan Principle, here's a tip for people using PowerBuilder against a SQL Server back end with single-sign-on.

Windows 7 Window Management

I'm one of the many who've been trying out the Windows 7 Release candidate - yes, I know that it's available retail now, it's on the Christmas list.

One thing that's bugged me is the apparent lack of follow through with Multi-monitor support. You see, if you drag windows around with the mouse, Windows 7 offers some useful auto-dock functionality, but only at the outer edges of the screens.

A Winforms Layout Puzzle

Here’s a strange piece of behaviour – a puzzle (with solution) for you.

Imagine that you’re working with Win.Forms, constructing a resizable layout using a TableLayoutForm as the host.

At Design time, your form looks like this (click to enlarge):

Designtime
Designtime

But, at run time it looks like this (click to enlarge):

Runtime
Runtime

The "Good Samaritan" Principle

Sometimes a quick consultation with Professor Google or Doctor Bing just doesn't bring you any satisfaction - and you end up having to work.

Richard Dingwall suggests that when this happens, you should consider blogging about the issue, sharing your experience so that others can benefit. (In many ways, this is a capsule description of all human progress!)

Testing and Static Methods

I'm working with a large codebase at the moment, trying to write unit tests to ensure that the changes I'm making aren't compromising the existing code.

For some of the code, this is easy. For other pieces, not so much.

One area that I'm finding difficult is in the area of static methods. Historically, I've been a fan of static methods for some scenarios. But, now I'm finding them a bit of a pain.

MappingLists with NHibernate

Here's a subtlety of NHibernate mapping that I ran into this morning.

When you map a one-to-many relationship as a list, you can have null values in that list when loaded.

How to Find Stuff

At this weeks Wellington .NET user group, James asked me how I find stuff - when I need to find out how to get something done, how do I find the information I need. Here's what I told him.

Exception Inspection

Watch $exception
Watch $exception

Here's a useful tip that I didn't know - you can use the pseudo-variable $exception in the Watch window to see the exception that's just been caught.

PhpMyAdmin and the White Screen of Death

After a recent hardware failure (*) I've been setting up a new LAMP sandbox, this time using Ubuntu 9.04 running in a virtualised environment.

Everything was going fine, but after importing a database backup into MySQL, PhpMyAdmin started misbehaving, giving a White Screen of Death after login. View source revealed an empty file.

DataBinding Goodness

There's a lot of "goodness" buried in WinForms databinding that doesn't get to see the light of day. Here's one example I discovered just today ...

How not to set up Tests

Some unit tests on a project were newly failing - I'd done the checkin immediately before the first failure, so it fell to me to sort out the problem.

Class Diagrams as Documentation

customer.png
customer.png

The Class Diagram tooling built into Visual Studio is worth its weight in gold as documentation.

What I didn't know, and discovered today, is that using Copy Image populates the clipboard with not only a raster version of the image, but a vector version as well. Fantastic for embedding into Word documents when writing documentation.

Strange Win.Forms behaviour

Here’s a strange piece of behaviour – a puzzle (with a solution) for you.

Imagine that you’re working with Win.Forms, constructing a resizable layout using a TableLayoutForm as the host.

Using Lambdas as Event Handlers

Of all the new features in C# 3.0, Lambda expressions have to be one of my favourites.

One non-obvious way that they can be used is as event handlers, in just the way that anonymous delegates could be.

Margin Guides in Visual Studio

To set a margin guide in Visual Studio requires a minor Registry Hack (*)

First, use RegEdit to go to the key HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Text Editor.

RegEdit Screenshot
RegEdit Screenshot

Next, create a new string value "Guides" with value "RGB(128,128,128) 99" (sans quotes). This creates a gray guideline just after the 99th column.

Change RBGB(r,g,b) to change the colour of the guide; you can create multiple guides by separating them with commas at the end.

(*) Usual warnings apply - stuff up your Registry and your machine is hosed. Take precautions.

Finding Configuration Files

Here’s a useful technique for finding the location of configuration files.

Each AppDomain has a base directory from which it loads assemblies – when you double click a WinForms or WPF application, the new process that starts up has an AppDomain whose base directory is the directory in which your exe is located.

Small Fonts

A recent question on StackOverflow.com asked Why use Monospace fonts in your IDE.

One of the answers referenced the use of Comic Sans as a font for coding. I couldn't quite believe it, so I tried it out - and found that Comic Sans is actually very readable at small font sizes, easy on the eye and very compact. Check this out:

fonts.png
fonts.png

(Click to enlarge)

WiX Techniques Updated

My whitepaper on WiX techniques has been updated to correct an error.

The previous version of the whitepaper omitted the definition of the "PreventDowngrading" custom action, necessary for proper upgrade behaviour.

The related blog posts (on versioning and upgrading) have also been updated.

Upgradable MSI installations with WiX

I had some interesting challenges when integrating WiX based MSI installer generation into my build scripts. My previous post (NAnt and WiX Versioning) details how I ended up injecting version numbers into the MSI build. In this post, I detail how to ensure that each newly generated installation file will cleanly replace the previous.

Databinding with Lists

A question came up recently on the NZ dot Net mailing list:

Any one use listbox.DataSource = myList for data binding to a List box?

I cannot refresh the data source "myList". If I use the following code to update the data source:

listbox.DataSource = null;
listbox.DataSource = newList;

The items in the listbox are object types rather than data values.

Can any one tell me how to refresh the data in the list box when binding to the control?

My reply was:

NAnt and WiX Versioning

In this post, I show how I've managed to integrate WiX into my NAnt build scripts while retaining consistency of versioning.

Outcome Based Thinking

Over on 43 Folders (great site, well worth a spelunk or two) there's a great post on getting focused.

I feel these techniques before, and find them quite useful - at least, when I remember to apply them. :-)

One key that has been really useful for me is to pay attention on how I phrase things I add to my TODO list - the template <em>I need to $FOO because I want to $BAR</em> is helpful here.

Conscious Development

I'm working on a couple of minor personal projects at the moment, and I'm using them as an opportunity to try and hone my skills at TDD/BDD style development. Even though I first heard about TDD some years ago, I haven't yet made the breakthrough where it becomes automatic. I guess I'm still at the conscious competence stage of skill acquisition.

Single Instance Applications

The .NET framework has lots and lots of built in goodies that aren't well enough known - like built in support for ensuring only a single copy of the application runs at a time. Too often, developers spend ages coding up support for operations that are already there!

Powerful Lists

As a part of the infrastructure introduced in .NET 3.5 to support the various flavours of LINQ, there are a whole heap of generic extension methods that are available whenever you have IEnumerable<T>.

Two that I've found useful recently are Intersect() and Except() - both of these work to filter values out of the sequence.

A couple of examples are the best way to understand them:

Extension Methods and Null Pointers

I'm sure that most of you know that you can't call methods on a null reference, so you'll expect that this code will fire a nasty exception:

IPath p = null;
int l = p.Length();

Except, it doesn't.

Why?

Dual Monitor Visual Studio

I'm a big believer in the benefits of having dual monitors - in fact, I'm fortunate enough to have dual monitors on both the computers I use most often.

One of the challenges is to make good use of Visual Studio across both monitors. While the Visual Studio IDE is very good at maximizing the usefulness of a single monitor (by having various panes strategicly docked), it doesn't lend itself so well to a dual screen configuration.

A Settings Problem Solved

Following up on my previous post, I've found the cause of the problem.

Configuration Manager in Visual Studio Express

Update, August 2010:
The tip detailed here does not work for Visual Studio Express 2010

This one has been bugging me for a while, so I'm very glad to find a fix.

The defaults for Visual Studio Express have the Configuration Manager hidden, I guess on the justifiable basis that most users of VS Express would just be confused by it.

Copy Error Dialogs

Here's a quick tip I was shown by a friendly Help Desk guy this week (thanks, Ben!).

Next time you get an error dialog from some random piece of software, don't reach for a pen to write down the relevant details.

User Settings Gotcha in Visual Studio

Found an interesting trap when configuring settings for a screensaver I'm working on.

Conditional Compilation

Now here's a trick that I didn't know - you can use the [Conditional] attribute to suppress a method unless a given symbol is defined at compile time.

From the MSDN documentation:

Applying ConditionalAttribute to a method indicates ... the method should not be compiled into Microsoft intermediate language (MSIL) unless the conditional compilation symbol that is associated with ConditionalAttribute is defined.

Mostly of use when you're writing logging and other diagnostic code - indeed, major use in the framework seems to be in the Debug and Trace classes.

Renaming WPF Windows

So, I wanted to rename a window in a WPF application - a prototype I'm working on.

First, I rename the code in the .cs code-behind file. Then, I change the Class attribute on the root element of the xaml file.

 

Cool, thinks I. A quick compile, no errors, everything looks good.

But, when the application is run ... I got an error dialog saying “There is no source code available for the current location”:

wpf001.png

Eh? I thought the technology was called WPF, not WTF!