Extension Methods on Null Instances

6 January, 2009 § 1 Comment

I was reading some blog posts and came across one from Brad Wilson about a year ago. Extension methods are a pretty cool construct, allowing you to add instance methods to sealed classes easily. This brings with it a way to write code that can cause some weird readability issues. Often used is the static method String.IsNullOrEmpty. With an extension method you could add that method to instances and the code will work fine, even if the object is null.

Here’s a code sample:

using System;

public static class MyExtensions
   public static bool IsNullOrEmpty(this string str)
      return String.IsNullOrEmpty(str);

Now all Strings will have a method to check if they are null or empty. You can see that the readability would make you think that a NullReferenceException would be thrown if the object is null and the method wouldn’t get called. This is the odd part of extension methods.

The method will still get called, and the code will work like normal.

public static void Main()
   string str = null;
   bool strIsNull = str.IsNullOrEmpty();

INotifyPropertyChanged interface causing a XamlParseException?

22 December, 2008 § Leave a comment

I was working yesterday on a side project, and came across an unexpected exception when trying to debug the program. I hadn’t changed any of the XAML, but all of a sudden I was getting a XamlParseException with an ambiguous message when the program started up. It turns out that I was using the INotifyPropertyChanged pattern wrong.

If you have a property named ControlVisibility, then when you call PropertyChanged, you must make sure to create the new PropertyChangedEventArgs with “ControlVisibility” as the argument.

Shout out to Craig for helping me with this.

LDAP Basic auth over SSL using S.DS.P

5 December, 2008 § Leave a comment

I recently ran in to an issue where self-signed certificates were causing an LdapException to be thrown. The LdapException simply said “The LDAP server is unavailable.” Pretty ambiguous, huh? Well, long story short, the LDAP server had been configured with a self-signed SSL certificate, and that certificate wasn’t trusted on the machine I was using. How could I check this?

Well, there’s two ways to check this.

1.) I opened up the certificate manager and checked. Using Vista, click the Start menu and type “certmgr.msc” in the text field. Hit Enter and it will bring up the Certificate Manager for the machine. You can look for the certificate within a number of the folders shown.

2.) I added a callback method to the LdapConnection.SessionOptions variable. This turned out to be a necessary add for the future because I can now fully understand and report to a user why the connection was bad.

« Read the rest of this entry »

C# Action delegates with Lamda expressions

19 November, 2008 § Leave a comment

The other day I was writing a program from a top-down approach. I don’t usually write code like this, unless I’m doing some test driven development. TDD is interesting because it puts the design over the implementation. By top-down I mean that I first wrote the Main method, then worked on fleshing out all the classes that I had envisioned from there. One of my main goals was for very readable code without comments.

My main method looked like this:

public static void Main()
  var carLot = new CarLot( "cars.xml" );
  carLot.Each( car =>

« Read the rest of this entry »

The Behavior of a Using Statement

14 November, 2008 § Leave a comment

C# comes right out of the box with it’s own garbage collector. When objects are no longer referenced and go out of scope, they are then marked for garbage collection. Some of these objects may have special properties.

They may carry handles to files, connections to databases, etc. When the garbage collector picks these objects up, it doesn’t really know what to do with these associations.

Cleverly, there is an interface to solve this. It’s called IDisposable. With IDisposable, you are required to implement one function, Dispose(). Dispose can be compared to the C++ destructor. File buffers and database connections can all be closed right within Dispose.

This seems nice, but awkward to call Dispose on an object when you are done with it. It would be nice to just nest that object in a block statement so it is easier to see from a glance when a specific object is not needed. It would also be nice if that Dispose method could be called when the block is finished.

This brings us towards a using statement. Here is an example:

ResultSet resultSet;
using( var sqlConnection = new SqlConnection() )
  resultSet = sqlConnection.ExecuteQuery( "select * from `products`" );
  sqlConnection.ExecuteNonQuery( "delete from `products`" );
...  //messing around with the result set
return resultSet;

« Read the rest of this entry »

MSTest fails to mention ignored tests

14 November, 2008 § Leave a comment

The other day I was writing some unit tests for a project I was working on. At the time, these tests contained some credentials that I both didn’t want to check-in and also didn’t want to break the build at a later date if I ever changed my password.

The test looked like this:

public void TestLogIn()
  var username = "j.wein";
  var password = "123456";

  var authenticated = Directory.Authenticate( username, password );

  Assert.IsTrue( authenticated );

« Read the rest of this entry »

Where Am I?

You are currently browsing entries tagged with c-sharp at JAWS.