Have language features changed the way we work for the better?

Craig Murphy dropped a little coding challenge on his blog the other day. This prompted me to write the responses as below. I’ve tried to target each one towards using a particular technology as the framework has developed. Have a look and tell me which is your preferred answer, or propose a new one (but again try to target a language feature). I’d love to know who you why you choose that language feature over another. Option 1. With Linq private const string alphabet = "abcdefghijklmnopqrstuvwxyz";   static void Main(string[] args) { char c = Console.ReadKey().KeyChar;   var myChars = from character in alphabet where character <= c select character;   var fullRow = myChars.Skip(1).Reverse().Concat(myChars);   var myRows = (from character in myChars select ConvertToSpaces(character, fullRow));   var reverseRows = myRows.Reverse().Skip(1); myRows = myRows.Concat(reverseRows);   Console.WriteLine(Environment.NewLine);//Skip the ReadKey line foreach (string line in myRows) Console.WriteLine(line);   Console.ReadKey(); }   private static string ConvertToSpaces(char wanted, IEnumerable<char> characters) { return new string( (from character in characters select character == wanted ? character : ' ').ToArray()); } Option 2. With Generics   class Program { private const string alphabet = "abcdefghijklmnopqrstuvwxyz";   static void Main(string[] args) { char c = Console.ReadKey().KeyChar;   List<char> myChars = new List<char>(); foreach (char character in alphabet) { if (character <= c) myChars.Add(character); }   List<char> temp = new List<char>(Reverse<char>(AllButFirst<char>(myChars))); temp.AddRange(myChars); string fullRow = new string(temp.ToArray());   List<string> myRows = new List<string>(); foreach (char character in myChars) { myRows.Add(ConvertToSpaces(character, fullRow)); }   IEnumerable<string> reverseRows = AllButFirst<string>(Reverse<string>(myRows)); myRows.AddRange(reverseRows);   Console.WriteLine(Environment.NewLine);//Skip the ReadKey line foreach (string line in myRows) Console.WriteLine(line);   Console.ReadKey(); }   private static IEnumerable<T> Reverse<T>(IEnumerable<T> myChars) { Stack<T> reverse = new Stack<T>(); foreach (T character in myChars) { reverse.Push(character); } while (reverse.Count > 0) { yield return reverse.Pop(); } }   private static IEnumerable<T> AllButFirst<T>(IEnumerable<T> myChars) { bool first = true; foreach (T character in myChars) { if (first) first = false; else yield return character; } }   private static string ConvertToSpaces(char wanted, IEnumerable<char> characters) { StringBuilder result = new StringBuilder(); foreach (char character in characters) { result.Append(character == wanted ? character : ' '); } return result.ToString(); } } Option 3. Using IEnumerable   class ProgramDNv2 { private const string alphabet = "abcdefghijklmnopqrstuvwxyz"; static void Main(string[] args) { DiamondChars diamond = new DiamondChars(); diamond.chars = GetChars( Console.ReadKey().KeyChar); foreach (string line in diamond) { Console.WriteLine(line); } Console.ReadKey(); }   private static string GetChars(char MidChar) { string chars = alphabet.Substring(0, alphabet.IndexOf(MidChar) + 1); return chars; }   private class DiamondChars : IEnumerable<String> { public string chars;   #region IEnumerable<string> Members   public IEnumerator<string> GetEnumerator() { int index = 0; foreach (char character in chars) { yield return MakeLine(character, index); index++; } index--; foreach (char character in chars.Reverse()) { if (index < chars.Length - 1) { yield return MakeLine(character, index); } index--; } }   #endregion   private string MakeLine(char character, int index) { string line = new string(chars[index], 1).PadLeft(chars.Length - index);   if (index > 0) return line + new string(chars[index], 1).PadLeft(index * 2); else return line; }   #region IEnumerable Members   System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }   #endregion }   } Option 4. Simple recursion   class ProgramDNv1 { static void Main(string[] args) { string input = "abcde"; StringBuilder result = new StringBuilder(); BuildDiamond(0, input, result); Console.WriteLine(result); }       private static void BuildDiamond(int index, string input, StringBuilder result) { string line = new string(input[index], 1).PadLeft(input.Length - index); string line2 = new string(input[index], 1).PadLeft(index * 2); result.Append(line); if (index > 0) result.Append(line2); result.Append(Environment.NewLine); if (index < input.Length - 1) { BuildDiamond(index + 1, input, result); result.Append(line); if (index > 0) result.Append(line2); result.Append(Environment.NewLine); } } } Now add a quick comment below telling you preferred implementation and briefly why. Thanks.

More on WPF

I think the picture says it all for now.

WPF unleashed

Following the review on coding horror, I'm ordering Windows Presentation Foundation Unleashed (WPF) from Amazon. Have to see if its any good.

Occasionally connected clients

One thing that NO application developers seem to get right is support for regularly disconnected connections. If you try to use a cell phone and travel at the same time, you get a regular drop in connectivity and occasional disconnect. For example I currently have just reconnected to the network and received full connection. However thirty seconds and (according to Live Search on my mobile) as we enter the next cell, then my initial connection suddenly loses its ability to perform any IP traffic whatsoever. One swift(!) disconnect and reconnect I’m ready to try again.  One of the best applications for coping with this is, not surprisingly, Outlook.  In the background it will note the fact that the IP connection is there and suddenly take advantage of this, swinging into life and attempting to grab those connections back to email servers. DotNet network information In fact it is remarkably easy to add to your own applications as since DotNet 2.0 there is a whole network connectivity interface that lets you determine just how connected you are, see System.Net.NetworkInformation. When I first looked at this a few years ago I found that hooking into the NetworkChange event handlers,  and if you listen just to the the NetworkAvailabilityChanged event, then it arrives just a little too early. It’s fine for disconnections, but you often get told that the Network is connected before you have an IP address. In particular I recommend checking the IP address that’s now available on the NetworkAddressChanged event. this can be used to dig right down. You can check the type of connection and even the subnet you've attached to. With power comes responsibility The question to consider at this point is how nicely should we play, when network connectivity becomes sparse how should we share it? For years I used a mobile cell tariff that didn’t include data, and positively prohibited it at its rate per Mb. My problem was literally killing off all those inconsiderate applications that were about to downloading as soon as they had the chance. A lot of programs that are better now as they at least only check for new updates once a week, but the question is how considerate should our applications be. Most users would want to I am writing the latest anti-theft program that will send those few bytes out telling people who has stolen their mobile device laptop then we need to wait before connecting to the internet. Unless Windows 7 comes along and provides means of categorising your data usage then we shouldn’t we provide a dialog that lets the user choose what to do with the connection when you first connect to it, maybe something like this

Querying LDAP with multiple domains

I'm trying to query an ActiveDirectory but at my current client site, I have a problem. There's a few. The client has previously migrated their DC from XPERT to EUROPE, and now they've bought a division of another company and added that in as well. As a result the default DC only supplies half of the staffs email addresses. Fortunately you can override the default DC by the use of,  DirectoryEntry root = new DirectoryEntry("LDAP://DC=SUBDOMAIN,DC=DOMAIN,DC=com"); Just update DOMAIN and SUBDOMAIN to where you are. More details on http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.searchroot.aspx

A Bluetooth triangle

I’ve recently bought a Jabra BT3030 . My excuse was so that I have a hands free for in the car, instead of putting the phone on speaker and leaving it in front of the gear stick (which works surprisingly well). However being the classic developer, I'm not just a power user, but a 'use it to destruction' user. This morning I had the opportunity to really test it out. I was already listening to music and reading the web when the phone rang. l love me way you do a crappy scribble on a tablet and just stick it in as an acceptable webpage diagram. My only complaint with my B73030 is that I’m having problems connecting it so that it can play music from either the phone or the laptop. It only accepts an A2DP stream from the last one it was paired with. I’ve contacted Jabra support and been told There is no upgrade for the BT3030. and it should be working. So please tjeck if there are there are new firmware updates for the phone. After much searching I’ve found the update, shame about the page error that’s blocking me from updating!


So my first major Linq project is nearly under my belt and l just have to say how much I like LinqPad. This is both a great learning and debugging tool. My favourite part of the site is the LinqPad challenge with the encouragement to "Delete the shortcut to SQL Enterprise Manager". I haven't but LinqPad is now pinned to the top of my start menu way above Enterprise Manager Download LINQPad

Definition of annoying

Your account is locked out. But it worked before I left the office!

Heaven is having your hardware back

A short while ago I had a minor problem Tablet Hinge popped and since then I’ve been suffering Missing my tablet. But the good news is its back. In seven days, including a delay while they gathered parts. I’m very impressed and very happy.

Missing my tablet

My tablet PC is being fixed at the moment and boy am I missing it, but strangely for all the wrong reasons. Keyboards You never realise just how used to a keyboard until you use a different one. Now I normally use two a day, one on the work PC and that on my tablet. Since my tablet has a US keyboard and everything else is UK I already have to do things differently, but today I'm struggling. The big reason is that I borrowed Melissa's Dell XPS M1330 and the keyboard on that is just not right. The first difference is that the keyboard has keys in very different places, and I've been coding all day. Home, End, Page up, just aren't in the same place as anything I've used before. That includes Toshiba, IBM/Lenovo and normal desktop. Worse the key width is just a little bit bigger than a normal keyboard such as the one I'm using right now. The number of times today I've type *( instead of (). However the greatest problem I've had is with the Shift and Ctrl keys. These keys are a bit wider than most other keys and unless you hit them in the centre, they don't go down. I've typed and corrected [ more times than I've typed {. Widescreen I bought the last non-Widescreen laptop that Lenovo make, and with Visual Studio as my primary application I have my windows docked just where I want them. I go for a two column, with an extra row across the main window [At this point I would scribble a little diagram to show you I mean if I had my tablet]. It just doesn't fit on a Widescreen machine. Maybe I've got spoiled by 1400x1050, but 1280x800 just doesn't allow enough vertical space to have the errors or a callstack showing at the bottom while still seeing a useful amount of code above. Strange really since my previous laptop was 1024x768. Weight and Balance The nice thing about a tablet is that the unit is a single flat piece of technology and so (convertibles in tablet mode, slates all the time) the balance is much easier than using an opened laptop on your knee. I've got so used to relaxing in the evening with my tablet on my knee, I'd forgotten that the screen helps those normal laptops topple backwards. Fortunately I didn't drop it though. Tablet Yes I miss the pen for pointing, clicking and hand writing, but it these other factors that caught me by surprise. Hurry up Lenovo. I really love my Tablet.