Wednesday, December 30, 2009

Getting Non-Null Data Redux

It has been pointed out to me that my previous post about this topic is a bit out-dated. Yes, it is and I did mention in that post that the code I posted was from an old 1.1 class that I never bothered to update when things like extension methods came along in subsequent .NET versions.

Well, I guess I’ve been chastised enough for it, so I’ll post some new code using extension methods. But first, I do want to say something about extension methods. They are cool, they make some things much easier (as I’ll illustrate at the end of this post) but they can also be over-used and therefore abused (IMHO). When over-using extension methods, it may be very easy to forget that these new methods are not native to .NET, that you (or another developer on your team) has created them. But, I suppose that will be brought home to you when you work on other apps that don’t have the extension method code in them … it just may take you awhile to remember, “Oh yeah, Intellisense isn’t showing this method because it isn’t there natively. Darn!”

One other note about the previously posted code (which I will correct in that post), is that I used code like this:

if (Test != DBNull.Value && Test != null)

When, of course, it should have been the other way around, silly me:

if (Test != null && Test != DBNull.Value)

So, without further ado, here’s the revised version using extension method (and really, all that has to be done is to add “this” to each method signature and I also changed the name of the class):

public static class BBExtensions
{
public static object GetNonNull(this object Test, object Default)
{
if (Test != null && Test != DBNull.Value)
return Test;
else
return Default;
}
public static string GetNonNull(this object Test, string Default)
{
if (Test != null && Test != DBNull.Value)
{
if(Test is DateTime)
{
DateTime TestDT = Convert.ToDateTime(Test);
DateTime SqlNull = new DateTime(1900, 1, 1);

if(TestDT == SqlNull)
return Default;
}
else if (Test is bool)
{
bool YesNo = Convert.ToBoolean(Test);
if (YesNo)
return "Yes";
else
return "No";
}
return Test.ToString().Trim();
}
else
return Default;
}
public static int GetNonNull(this object Test, int Default)
{
if (Test != null && Test != DBNull.Value)
return Convert.ToInt32(Test);
else
return Default;
}
public static DateTime GetNonNull(this object Test, DateTime Default)
{
if (Test != null && Test != DBNull.Value)
{
DateTime TestDT = Convert.ToDateTime(Test);
DateTime SqlNull = new DateTime(1900, 1, 1);
DateTime NetNull = new DateTime(1, 1, 1);
if(TestDT != SqlNull && TestDT != NetNull)
return TestDT;
else
return Default;
}
else
return Default;
}
public static string GetNonNullDate(this object Test, string Default)
{
if (Test != null && Test != DBNull.Value)
{
if(Test is DateTime)
{
DateTime TestDT = Convert.ToDateTime(Test);
DateTime SqlNull = new DateTime(1900, 1, 1);
if(TestDT != SqlNull)
return TestDT.ToShortDateString();
}
return Default;
}
else
return Default;
}
public static DateTime GetNonNullDate(this object Test)
{
if (Test != null && Test != DBNull.Value && Test is DateTime)
return Convert.ToDateTime(Test);
else
return new DateTime(1900, 1, 1);
}
}

So, now why is this better than the old CommonFunctions class I had? Well, here’s how you had to use the old class:

int MyInt = CommonFunctions.GetNonNull(MyDataSet.Tables[0].Rows[0]["MyColumn"], 0);

// -or-

DateTime MyDatetime = CommonFunctions.GetNonNullDate(MyDataSet.Tables[0].Rows[0]["MyDateColumn"])

And here’s the extension method way of doing this, a bit cleaner:

int MyInt = MyDataSet.Tables[0].Rows[0]["MyColumn"].GetNonNull(0);

// -or-

DateTime MyDatetime = MyDataSet.Tables[0].Rows[0]["MyDateColumn"].GetNonNullDate();

4 comments: