Tuesday, September 15, 2009

Age Extension Methods for .Net DateTime Type

There is not really a straightforward method to calculate a person’s age using the .Net DateTime type. Subtracting years is not a correct means. What is required is to round down a person’s age until that person’s birthday has passed for the given year it is being calculated against. This is what these simple set of extensions to the .Net DateTime class intend to provide.

Monday, September 7, 2009

IsNullOrEmpty Extension Method for .Net Collections

One of the methods on the string class in .Net framework that was added in .Net 2.0 was the IsNullOrEmpty method. This was implemented as a shortcut mechanism for what developers had been doing which was something like:
if (str == null || str.length == 0) {
//do something
}
Or probably more often
if(!(str == null || str.length == 0)) {
//do something
}

If you think about it the string class in .Net is just a specialized type of collection. I see myself performing that similar pattern for other types of collections in .Net. Why not create an IsNullOrEmpty method to support the .Net collection classes and array types?

It is a pretty simple extension and I plan on incorporating this function in any new .Net 3.5 work that I do. It is just a handle shortcut to use.

Saturday, August 29, 2009

.Net Extension Method to Load ASP.Net ListControls from Xml

Several months ago I joined a group of developers who were in the middle of development of an enterprise web application. What the application does is not as important as some of the interesting implementation techniques they used. One of them was the loading of static, common ASP.Net ListControls from embedded Xml files. I am talking things such as Yes/No radio lists, a list of states, a list of counties, etc. You know, for things that are not going to change at all or not very often.

In browsing through their code, the one thing that caught my attention was that they had developed their own Xml schema to define the items in the lists. My initial thought was, why not use an Xml schema format that allows one to use the Xml serialization functionality of .Net and not have to go through the effort of performing that translation yourself?

So that’s my experiment in this post. Create an Xml file format so Xml serialization can be used, and since I am using .Net 3.5, use extensions methods to handle the translation instead of developing a special purpose utility class to handle that for me.

Here is the extension class used to translate the Xml file into the ListControl. A ToXml method is provided just as a means to obtain a copy of what the Xml schema will look like for a ListControl.



An embedded Xml file that includes a Yes, No and Maybe option with the Maybe option being the defaulted selected item.

<?xml version="1.0"?>
<ArrayOfListItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ListItem>
<Text>Yes</Text>
<Value>yes</Value>
</ListItem>
<ListItem>
<Text>No</Text>
<Value>no</Value>
</ListItem>
<ListItem>
<Text>Maybe</Text>
<Value>maybe</Value>
<Selected>true</Selected>
</ListItem>
</ArrayOfListItem>



This image shows the properties that need to be set to embed an Xml file in a Visual Studio project



This example shows how to load an embedded Xml file into an ASP.Net ListControl on the Page_Load method of an ASP.Net page

Sunday, June 28, 2009

Forth as a Web Service

This is my latest iteration of developing a Forth system. I started with Forth in .Net, Forth interpreter in JavaScript, Forth to script Google Street View and now Forth as a web service. This latest work builds upon a Python web service framework I developed. It is hosted in the Google App engine and the Forth interpreter is a Python port form my JavaScript Forth implementation.

What I like about this implementation is that I now have a Forth system that is accessible to other systems. Also it stores the state of the Forth interpreter between calls. This allows users to develop Forth programs still using the iterative Forth programming style. It stores state by persisting the stacks, and the user dictionary using the Google App Engine data store. These structures are pickled using Python to ease storing and retrieval. Developers can choose to pass in a session value to the web service to identify a Forth interpreter instance to use. This is beneficial so you do not have multiple callers manipulating the state of the same Forth interpreter instance and getting unpredictable responses back.

To the Forth Web Service Console