Skip navigation

As I mentioned before (I think), my focus on UI development has been a recent phenomenon in my career. Prior to that, the majority of my efforts has been in back-end frameworks and integration. Even looking at what I’ve done in the WPF arena, most of my attention has been on creating utilities that do a lot of behind the scenes work: MVPoo, Commands, Attached Properties, and all that fun stuff. I guess it’s only natural that I land back in the arena of back-end and framework development. I’ve got a few interesting classes and utilities to share from my recent excursions in the area. Hopefully, someone will find these useful.

First up are a couple of extension methods for range validation. Right now, they only work on IComparable<T> (which all value types derive from). Being extension methods, they only work with .NET 3.5 but it is an easy exercise to change them to standard static utility methods. The interesting bit I discovered writing these is that extension methods can be Generic. The unfortunate part is the redundancy required here…something that Jeff Atwood would probably frown upon. Here is how the method would be used:

int tmpInt=45;
bool isValid=tmpInt.ValidateRange<int>(25,50);

The ValidateRange methods (I overloaded the function so that a caller can specify whether the range should be inclusive) take the type of the object being validated as a type parameter so that it can pass it to IComparable<T> and strongly type the min and max parameters. I guess I could just use the non-generic IComparable interface instead of IComparable<T>, but then you’d lose the compile time verification.

Anyway, the code is attached below. Let me know what you think.


One Comment

  1. very nice! Looks like you\’ve got a cut & paste mistake on the first method though: ValidateRange<T>(value,minValue,minValue,true); the 2nd minValue should be maxValue, right?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: