After being tagged my numerous people, here's my 5. My
long-time readers might know some of these if they've been reading closely over
the past few years, but here are 5 things about me that aren't widely known:
- In college, I was a member of the Texas
A&M country & western dance team: www.aggiewranglers.com – I got to travel
the country and world performing c&w dance. I married a girl who was also
on the team. (all partner dancing, people, no line dancing). Think "girl being
thrown in the air".
- In 2003 I was
called up for Army service to Iraq – was there 1 year and 3 days. I'm 1.5 months
from separation, so I'm hoping the nothing BIG happens between now and March
3rd.
- I was using Yahoo before it had
e-mail service, and my account is one of those that is really jacked up because
my id I originally registered had to be an e-mail address.
- I started writing database web applications
before the dawn of ASP 1.0 – Anybody remember .idc and .htx files? – My CGI work
was configuring WODA for a few clients.
- When I was a kid, I read every book on
motorcycles/dirtbikes/ATVs in the public library, and I'd moved on to the adult
section. I was the only young kid who knew the difference between a two-stroke
and four-stroke engine. I now have a motorcycle, love to ride dirtbikes, and I'm
lobbying my wife for a Harley.
Now, I'm going to tag Scott Bellware 5 times.
So
that this post is now void of technical content, here is a tip for using
enumerations in your applications:
Consider the following class:
1 public class WorkOrder
2 {
3 private Status
_status;
4
private string _description;
5
6 public Status
Status
7
{
8
get { return _status; }
9 set { _status = value; }
10 }
11 public string
Description
12 {
13 get { return
_description; }
14 set { _description = value; }
15 }
16 }
Notice that we are using a
Status class (enum):
public enum Status
{
Pending,
InProcess,
CompleteNApproved,
}
My system manages work
orders. Realize that this is an over-simplified example, but what happens when
you need to list our many WorkOrder(s) in a grid or for a printable report?
Easy, right? No. You have this Status property that is very ugly when
displayed. "InProcess", "CompleteNApproved". Your users don't want to see
that. In business speak, it needs to be displayed as "In-Process", and
"Complete & Approved". How do we do this easily in code? On possible way
is to create a Facade class that is used in reports. This facade class could
translate the enum to our required Status titles. This is undesirable because
we then have to wrap each WorkOrder instance in a new class just for display
purposes. I would prefer that our WorkOrder could be displayed on its own, but
we have to do something about that enum. Let's turn our enum into an enumerated
class:
First, I'll rename the enum like so:
public enum StatusValue
{
Pending,
InProcess,
CompleteNApproved,
}
Then I'll make Status a
class with 3 static instances:
public class Status
{
private static Status
_pending = new Status(StatusValue.Pending, "Pending");
private static
Status _inProcess = new Status(StatusValue.InProcess, "In-Process");
private static
Status _complete = new Status(StatusValue.CompleteNApproved, "Complete & Approved");
private StatusValue _statusValue;
private string _displayTitle;
private Status(StatusValue statusValue, string displayTitle)
{
_statusValue = statusValue;
_displayTitle = displayTitle;
}
public StatusValue StatusValue
{
get { return
_statusValue; }
}
public string
DisplayTitle
{
get { return _displayTitle; }
}
public override
string ToString()
{
return _displayTitle;
}
public static Status Pending
{
get {
return _pending; }
}
public static Status
InProcess
{
get { return _inProcess; }
}
public static Status Complete
{
get {
return _complete; }
}
}
Notice that I've overridden the ToString() method
so that my _displayTitle field is returned. Now, when I throw my WorkOrder
class (or List<T> of instances) into a grid or some other binding
container, I'll see the proper display title for my Status. Now my users are
happy, and I can move a WorkOrder class around anywhere in my application. For
persisting to the database, I use the StatusValue (which can be Int32, Int16, or
byte) in my database table. It's easy to do that whether I'm using a
hand-crafted SQL statement or (my favorite) NHibernate for my ORM
solution.
Our lack of built-in enumerated classes is something I plan to
gripe about at the up-coming MVP Summit. Our enum construct is very weak and is
no more than glorified primitives. Consider what Java has as of version 5
(taken from http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html):
public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7),
PLUTO (1.27e+22, 1.137e6);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double mass() { return mass; }
public double radius() { return radius; }
// universal gravitational constant (m3 kg-1 s-2)
public static final double G = 6.67300E-11;
public double surfaceGravity() {
return G * mass / (radius * radius);
}
public double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
}
Now we need to play catch-up again. If in C# I was able to declare my
Status enum like this, I could give it a friendly display title and even some
methods right there in the enum definition. I would have less code to maintain,
and I would get the build in enum behavior for free.