Community developer blogs

Charles Cook

Website

Recent Posts

  • Linq and Functional Programming

    Posted: 03 Jul 2009 at 16:10

    I'm writing some code to display some old blog posts and after retrieving each post as a single string containing CR-LF separated lines I needed to split the string into individual lines and wrap each line with a <p> tag. I could split the string using String.Split but I wondered if it was possible to use a StringReader to generate an enumeration of the lines in the string.

  • Why No Top-Level Functions in C#

    Posted: 24 Jun 2009 at 09:13

    I've speculated for a long time about why C# doesn't have top-level functions, for example in this post, where the solution to the problem is rather ugly because the static functions have to be qualified by their class name, i.e. instead of this: Rgx.Expr e = Rgx.Seq(Rgx.Char('c'), Rgx.Seq(Rgx.Plus(Rgx.Alt(Rgx.Char('a'), Rgx.Char('d'))), Rgx.Char('r'))); It would have been nicer to write this: Expr e = Seq(Char('c'), Seq(Plus(Alt(Char('a'), Char('d'))),

  • Evening in Upper Dearne Woodlands

    Posted: 14 Jun 2009 at 22:16

    One of my favourite evening hikes after a day working at home is to park at Denby Dale, walk up to Upper Denby through Hagg Wood, then round to Square Wood Reservoir and up to the old Quaker settlement of High Flatts, then down to the Upper Dearne Woodlands via New House, and so back to Denby Dale. Beautiful views of the Dearne valley during the first half, then back through the woods which are particularly beautiful in the evening sunlight. About 4 miles.

  • Functional style regex engine in F#

    Posted: 09 Jun 2009 at 22:01

    Nick Palladinos has done a follow-up to my post Functional Style Regex Engine in C# Revisited. In his post Functional style regex engine in F# he presents just that, an F# version of my C# code: let char c (s : string) = seq { if s.Length > 0 && s.[0] = c then yield s.Substring(1) } let (=>) l r s = seq { for sl in l s do for sr in r sl -> sr } let (<|>) l r s = seq { yield! l s; yield!

  • Running VMWare Fusion on iMac

    Posted: 07 Jun 2009 at 20:47

    I've been running Windows 7 RC as a VMWare Fusion guest machine for a week or so now. Windows 7 requires 1GB of memory and I was experiencing a lot of paging on my iMac, which only has 2GB of memory, when I was trying to run several other applications in Mac OS X at the same time. So I ordered a 2GB SO-DIMM and I just installed it. Things are running much more smoothly now.

  • NOptFunc

    Posted: 06 Jun 2009 at 16:46

    A few days ago Simon Willison posted about his optfunc command line parsing program written in Python: Command line parsing libraries in Python such as optparse frustrate me because I can never remember how to use them without consulting the manual. optfunc is a new experimental interface to optparse which works by introspecting a function definition (including its arguments and their default values) and using that to construct a command line argument parser.

  • List&lt;T&gt; Enumerator Gotcha

    Posted: 04 Jun 2009 at 16:56

    I find I am using sequences and iterators much more these days because of the influence of Linq. Even when using manipulating arrays this often results in more robust code because you don't have to worry about boundary conditions with indices; and it is easier to pass around an iterator rather than an array and a reference to the current position within the array, or so I thought until I came across an issue with List<T>.

  • Answer to "NUnit isn't running VS10 code"

    Posted: 31 May 2009 at 18:12

    I just posted my first answer to stackoverflow. Brian Ball was getting the following error when trying to load a dll built using VS 2010 beta into the NUnit GUI: This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. You may be attempting to load an assembly build with a later version of the CLR than the version under which NUnit is currently running. My answer was: I've downloaded the NUnit 2.

  • Injected Type Needs to Know Type Being Injected Into

    Posted: 30 May 2009 at 10:10

    Injecting a logger into an object is a frequently used example of DI. I have a case where the logger I want to inject needs to know the type of the object it is being injected into, so that as part of its output it can log the type from which each log line is being output. For example in the code below, where the constructor of type Foo has a parameter of type ILogger and where the implementation of ILogger - the Logger class - has a Type parameter. class Logger : ILogger {

  • Splitting a String

    Posted: 30 May 2009 at 10:10

    I recently investigated a bug where someone had tried to split a string into space separated words using String.Split in C#. The bug was that the code failed to handle leading/trailing spaces and sequences of more than one space in the string: var input = " 1 22 3333 "; var words = input.Split(' '); foreach (var word in words) Console.Write("[" + word + "] "); // outputs [] [1] [22] [] [3333] [] []

  • Windows 7 RC in VMWare Fusion

    Posted: 30 May 2009 at 10:10

    I installed Windows 7 RC in VMWare Fusion today (on an iMac with 2G of memory running Mac OS X 10.4). I followed the instructions on this post on the Team Fusion blog. "Windows Easy Install" blue-screened the guest machine so I tried a custom install which worked fine. I then installed the Visual Studio 2010 beta and found this was very sluggish with a lot of display problems. Configuring the guest machine with 3D display switched off improved the performance and fixed the display issues.

  • Alternative Syntax for Member Calls on C# Dynamic Types

    Posted: 30 May 2009 at 10:10

    XML-RPC.NET is essentially concerned with making statically typed calls to XML-RPC endpoints, using interfaces as contract definitions in a similar way to WCF. However the new dynamic type in .NET 4 makes it possible to provide a clean way of making dynamically typed calls, for example like this: dynamic client = new XmlRpcClient(" returnValue = client.Add(2, 3); The DynamicObject class in the System.Dynamic namespace makes implementing a dynamic class straightforward: using System.

  • Functional Style Regex Engine in C# Revisited

    Posted: 30 May 2009 at 10:10

    In January 2007 I posted about a Functional Style Regex Engine in C#. This was an exercise in using iterators and anonymous functions in C#. I decided it was time to see if it was possible to rewrite the code using Linq. To recap, the goal is to be able to specify a regular expression in a functional style like this: // c(a|d)+r Rgx.Expr e = Rgx.Seq(Rgx.Char('c'), Rgx.Seq(Rgx.Plus(Rgx.Alt(Rgx.Char('a'), Rgx.Char('d'))), Rgx.Char('r')));

  • Tip: Convert Value or Reference to Sequence of Length One

    Posted: 30 May 2009 at 10:10

    In the previous post I originally created the sequences based on a single value by creating an array. For example: static Expr Nil() { return s => new string[] { s }; } However this deviates a bit from the spirit of Linq, adds an unneccessary implementation detail, the fact that an array implements IEnumerable<T>, and hard-codes the string type into the expression. The neater way to do it is to use Enumerable.

  • Logging of Unity Resolution Failure

    Posted: 06 Mar 2009 at 09:51

    When using the Unity Dependency Injection container I've found that when a resolution of a dependency fails the Message property of the resulting ResolutionFailed exception is not immediately useful if there is more than one level of dependencies being resolved. For example this code: using System; using System.Collections.Generic; using Microsoft.Practices.Unity; interface ILogger { } class Foo { public Foo(ILogger logger) { } } class Bar { public Bar(Foo foo)

  • Unit Testing Custom Config Section

    Posted: 15 Feb 2009 at 15:38

    I recently had to write some unit tests to test a custom configuration section. I was using NUnit so I didn't want to use the config file that would be loaded by NUnit, for example nunit-gui.exe.config, but instead wanted to load one or more separate test config files. The solution was to use the ExeConfigurationFileMap class. For example, say I have this config file: <configuration> <configSections> <section name="MySettings" type="MySetti

  • Spike

    Posted: 11 Feb 2009 at 17:44

    When I was at the last DeveloperDeveloperDeveloper day I came across usage of the word "spike" in software development for the first time. Extreme Programming has this definition: Create spike solutions to figure out answers to tough technical or design problems. A spike solution is a very simple program to explore potential solutions. Build a system which only addresses the problem under examination and ignore all other concerns. Most spikes are not good enough to keep, so expect to throw

  • Fowler on Dependency Injection vs Service Locator

    Posted: 08 Feb 2009 at 15:34

    Following my last post on Dependency Injection Andy McMullan posted a comment to my Twitter feed: Just read Fowler's IOC/DI page - seems he disagrees with Prasanna about the superiority of DI over Serv Locator. In the article Inversion of Control Containers and the Dependency Injection pattern Fowler compares Service Locators and DI and presents some arguments for and against DI compared with Service Locators: For DI: Easier to determine what dependencies a component has - look

  • Dependency Injection

    Posted: 05 Feb 2009 at 09:13

    I've become a big fan of Dependency Injection over the last few months. I was adding new functionality to a large .NET application which had not been designed with unit testing in mind and I found it difficult to create unit tests for the new components I was writing. I had to use a ServiceLocator type mechanism which could be configured during unit testing to supply mock types. Getting this into place required a substantial amount of work on top of the new functionality I was implementing. I am

  • Converting Locale ID (LCID) to Locale Name

    Posted: 05 Feb 2009 at 00:02

    While in the vicinity of System.Globalization, a quick tip, something I used the other day for the first time, on how to convert from a Locale ID, or LCID, to the corresponding locale name: just use the constructor of the CultureInfo class which takes an LCID: CultureInfo ci = new CultureInfo(0x809); Console.WriteLine(ci.Name); Assert.AreEqual("en-GB", ci.Name); And going the other way: CultureInfo ci = new CultureInfo("en-GB"); Console.WriteLine("0x{0:X}", ci.LCID); Assert.AreEq

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

Related blogs

  • Thushan Fernando Uncut

    Not Even Remotely Dorky thoughts from a Windows/Linux, .NET, C/C++ & Java software developer. From computer software, programming and hardware to motorshows and travel to various countries.

  • good code

    It's time to start being less reactive and more proactive

  • عفیف احمد جنجوعہ

    Post related to my personal experience with the various frameworks, development tools and technologies, programming languages and libraries at hand. Development tools inlcude visual studio/ netbeans/ eclipse. Frameworks include .net/ Java. Languages c/ cpp/ php/ java/ c#/ asp.net. And various libraries and software factories

Related podcasts

  • CodeCast Episode 5: VSX, VSIP, Aggiorno, DBI-Tech Controls

    CodeCast Episode 5: VSX, VSIP, Aggiorno, DBI-Tech ControlsThis 5th episode of CodeCast includes host Ken Levy with a new segment on news and gadgets, and three interviews all related to VSX and VSIP (Visual Studio Extensibility and Visual Studio Industry Partners). Interviews are with Microsoft a...

Want to stay in touch with what's going on? Follow us on twitter!