Posts Tagged ‘.NET’

Why the default form instance does more harm than good

February 2, 2012 1 comment

First things first. The default form instance is a feature of VB.NET for versions 2.0 and up, applicable only to WinForms projects, and provides a forms management functionality that is in keeping with the code “feel” of VB6, the predecessor of VB.NET. Functionally, this feature allows for the programmer to use the name of a form to directly reference instance members on that form’s class (i.e. controls, properties). This feature made sense when working with forms in VB6

This of course flies in the face of object oriented design (and the .NET framework’s design patterns for that matter) which leaves the onus on the developer to be in control of his own object instances. In essence it introduces a non-OO principle into an OO language — and this is the starting point of my argument that in net sum, this is a harmful feature… at least in the way it is presented in the default WinForms Visual Studio template (I’ll clarify that later).
Read more…


When to use custom exceptions

June 12, 2011 Leave a comment

Have you ever found yourself in a situation where there seems to be no exception type that is a fit for the exception you want to throw? In smaller, stand-alone projects it is easy to just ignore the problem and use a plain old, generic Exception. After all, you’re the only one who’s going to see it, right? For larger, more complex projects or for class libraries that will be consumed by other assemblies that may not be written by you it can be useful or necessary to throw exceptions that have targeted meaning. The solution is to make your own, and the .NET framework has provided us the means. Read more…

Parsing large numbers to words (english)

May 8, 2011 1 comment

I’ve occasionally seen a request for this functionality pop up in the forums over the years. A developer is given the task to write out in words a number the way it may be spoken by a person. Actually, a more common use for this functionality is with bank checks where we write out the amount in words. So $1,234.56 would be written out as One Thousand Two Hundred Thirty Four Dollars and Fifty Six Cents. It’s amusing that the people who search for this functionality in code are surprised and even annoyed that the .NET framework does not have this built in… as if the framework does not have enough stuff already, right? A while back I came up with a solution for this based on a forum request (see the revised code at the bottom). I thought I would share this here again as a separate blog post. Actually, I decided to upgrade the code a tad just for the sake of never leaving good enough alone 😉
Read more…

The null object pattern in use

April 16, 2011 Leave a comment

What is the null object pattern and why should you care? Well, if you’re a masochist who likes to add superfluous validation code to any give project or if you are an obsesive compulsive about adding such superfluous code… then I suppose you wouldn’t care. However, for the rest of us this can be a quite useful design pattern that can lead to simpler code that is more maintaianable and better testable. That covers the why, so here is the what in a nutshell.

Design patterns provide an architectural or algorithmic set of steps or procedures to solve specific programming design issues. This design pattern addreses the common issue with .NET development where an instance of a class, or a reference object, is tested for having a null reference before using the object in code. Why would you do this in the first place? Consider that if you attempt to access a method or a property on a reference object that is null (or Nothing in VB.NET) you will encounter a nasty NullReferenceException. This is not the type of error you would want to introduce to your user, but even if you are savvy enough to catch such an error before it crashes your application you are still posed with the issue of restoring the normal flow of operation for that application in light of this unexpected error. Read more…