Friday, February 24, 2006

Undocumented regular expression pattern

UK Postcode validation is the theme for me today, and the standard has been very useful. Digging a little deeper even provides a couple of regular expression patterns for doing the validation with, both a simple one and a more complicated one.

Examining the patterns has led me to a construct that I can't find documented anywhere. I've looked through all the linked pages in the "Regular Expression Language Elements" section in the VS2005 help, but with no joy. It seems to work though.


Fairly self-explanatory hopefully, with this particular example matching any one of twenty capital letters. If it is actually documented somewhere, please do point me at it.

Thursday, February 23, 2006

Multiple keys with KeyedCollection

I'm currently dealing with some objects that have two different unique identifiers. One is a public, user-friendly identifier, and the other is an internal, essentially meaningless one. Meaningless, that is, other than the fact that I can ensure that it stays the same for all time, as opposed to the one that users can see, which therefore will no doubt need to be altered 30 times as random punter X changes their mind yet again.

As a result I wanted a collection where it's possible to grab an item quickly and easily using either of the identifiers. Deriving from KeyedCollection<TKey, TItem> does the job nicely for a single key, so I thought I'd extend that a bit farther and come up with my own cunningly named DoubleKeyedCollection<TKey, TSecondKey, TItem>.

To my shame, I've not even tested this code before posting it up here as it's already long past the end of the day, although it does compile, which is always encouraging if nothing else. I'm not hugely fond of the choice of GetItem() as a method name as it's a bit ambiguous, especially if your two keys are of the same type. GetItemBySecondKey() seemed a bit meaningless to an end client using a class derived from DoubleKeyedCollection though. Any thoughts on the subject are welcome (as is notification of any blatant errors that I've managed to completely overlook).

As an aside, I think I'll be examining the possibilities of changing the layout of this page. The narrow column doesn't exactly lend itself to code listings. I've done what I can to make it readable. EDIT: Template now changed - not as pretty imho, but makes the code a lot easier to read!

Thursday, February 09, 2006

Multiple search patterns with DirectoryInfo.GetFiles()

Another visit to Google today showed various requests about how to specify more than one pattern to search for when grabbing the files from a directory, so for example, get all the .mp3 and .wma files in one call.

As far as I can see, you can only specify one pattern at a time (unless there's something new in 2.0 that I've missed) so I thought this was a good candidate for a simple wrapper. The code below should be fairly easy to modify if you have any specific requirements in your situation.


A quick Google will tell you that this has been discussed plenty of times before, but seeing as I stumbled across it of my own accord today, I thought I'd give it a mention.

Set the property (the title of this post) to false, then display the dialog on a Windows 2000 machine. Whoops, the "New Folder" button is still there. Oh well.

Apparently it works on Windows XP - I guess you just have to live with it if you're running Win2k.