And I’m back at work again! I got a call from a friend of a friend on Canada Day needing some help with a PHP project. So it’s back to the grindstone for me!
Today marks the end of a six month contract with Redengine. It’s sad to leave, but I wish them the best with their future endeavors!
I’ll be spending the summer looking for work, working on stuff around the house and on personal programming projects under Hoffsoft Consulting Services Corporation.
Ever since I first heard about it on the Plumbers @ Work Podcast I’ve been looking forward to the release of the psake PowerShell build tool James Kovacs was developing.
Looks like one of the first versions have been released. I’m excited for two reasons: 1) command-line build tools are always the most powerful tools, 2) I can use this to learn PowerShell!
Anyway, thanks to James Kovacs! I’m looking forward to trying it out!
I just read a great article in MSDN Magazine (available online here) on the Open / Closed Principle from the Patterns in Practice column. I really enjoyed it. I’m going to have to spend some more time learning some more design patterns. I’ve spent some time learning Model-View-Presenter (MVP) and Inversion of Control (IoC) but haven’t put either into practice yet. Who knows what the future holds though?
Another new feature added in C# 3.5 to accommodate anonymous types is the introduction of the var type keyword. In his Coding Horror blog Jeff Atwood is extolling the virtues of using var to reduce type declaration redundancy. His example is converting:
StringBuilder sb = new StringBuilder(256);
UTF8Encoding e = new UTF8Encoding();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
into this:
var sb = new StringBuilder(256);
var e = new UTF8Encoding();
var md5 = new MD5CryptoServiceProvider();
His point is that declaring the type as StringBuilder and then assigning it a new StringBuilder is redundant - StringBuilder appears twice on that line of code, before and after the assignment operator (=). He makes an excellent point because the example using var is far more concise than the original example. Furthermore, we should be more interested in what is being assigned than what we’re assigning too (at least on that line of code). I find when I read the first block my eyes stick on the left-hand side of the assignment, but in the second example I read the right-hand side code instead.
As a side note. the excellent tool ReSharper 4.0 suggests using var for every local variable declaration by default. It’s one of the first things I changed in ReSharper when I started using it.
You see, I don’t entirely agree with using var. Using var is fine, but we aren’t converting C# into a dynamically typed language like Python or JavaScript - C# is still statically typed, using var just forces the compiler to figure out the type you’re using, usually based on the first value assigned to it. In Python or JavaScript I could take sb from the above example and assign to it the UTF8Encoding() object reference on the next line, but C# won’t let you do that, the compiler will give you an error.
I don’t like the illusion, so where possible I will use var, but I’ll keep using types too when necessary for clarity.
I just found out after tracking down a weird popup alert error “Object reference not set to an instance of an object” that it was being caused inside an ASP.NET asmx web service being called from a ComponentArt grid control. The web service was causing an exception but it was not caught by Application_Error!
I spent a few hours trying to find out where on the client the exception was occurring before tracking it to the web service. I figured nothing was happening on the server since Application_Error didn’t log the error for me.
So, unless you know something I don’t know, make sure you add your own error handling to your Web Services.
Finding common values in two lists is simple in .NET 3.5 thanks to LINQ! I found this example here and here.
In practice here is what I did:
var currentOperatorFacilityIds = from fac in Facility.Facility.ListAllForOperator(this.OperatorId) select fac.ID;
var deleteOperatorFacilityIds = from fac in Facility.Facility.ListAllForOperator(operatorId) select fac.ID;
var commonFacilityIds = currentOperatorFacilityIds.Intersect(deleteOperatorFacilityIds);
Really simple! I love LINQ.
I’ve been working on some ASP.NET projects lately and encountered something that was easy to solve, and should have been obvious straight off.
I was using a GridView to display some values including some columns that show dates. To edit the dates I wanted to use some controls from the AJAX Control Toolkit, specifically the MaskedEdit to format the input and the pop up Calendar control, so I had to use a TemplateColumn in the GridView and setup the EditTemplate with these controls.
I also wanted to format the date inside of the ItemTemplate to match the format of the other date BoundFields.
Even in ASP.NET 2.0 this was pretty easy, I just setup the template as follows:
<ItemTemplate>
<%# // If DueDate is null returns an empty string, otherwise formats and displays the date properly.
(null == Eval("DueDate")) ? string.Empty : DateTime.Parse(Eval("DueDate").ToString()).ToString("yyyy/MMM/dd") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="DueDate" runat="server" Text='<%# Eval("DueDate") %>' />
<tk:CalendarExtender ID="DueDateCalendar" runat="server" SkinID="Calendar" TargetControlID="DueDate" />
<tk:TextBoxWatermarkExtender ID="DueDateWatermark" runat="server" SkinID="DateWatermark" TargetControlID="DueDate" />
<tk:MaskedEditExtender ID="DueDateMaskedEdit" runat="server" SkinID="DateMask" TargetControlID="DueDate" />
</EditItemTemplate>
The biggest thing was checking for null in the Eval statement for the ItemTemplate. I also setup the ObjectDataSource to ConvertEmptyStringsToNull.
I had a problem when my CakePHP session wasn’t saving from page to page and it turned out the session ID (from session_id()) was changing from page to page also.
It turns out to be a simple problem: I had pointed Apache to my app folder not my app/webroot folder.
This also caused webroot to be inserted into all my URLs as well.
Quick and easy fix. Long frustrating time to figure out. 
I found this really great resource for getting into some of the CakePHP 1.2 helpers. They’ve changed things from 1.1, especially the FormHelper.
Check it out here: http://www.donutczar.com/cake1point2/donuts/