AddressInfo.cs: Converting State Codes to State Names and Vice Versa

0 comments suggest edit

I’m feeling a little uninspired to write anything interesting because my hands are hurting from the keyboard pounding I’ve been doing. Instead, I thought I’d dig up this really simple (and hopefully useful) AddressInfo class for you. The code is extremely basic, but it might save you the hassle of typing all fifty states (and some territories and the District of Columbia) and their two letter postal codes into your own class because I did it for you! Free of charge!

If I save one hand out there, my work here is done. The basic premise is this, the U.S. isn’t adding states to the union very often, so it makes sense to have the list of states and state codes as an enumeration rather than a lookup table in the database. Makes it easier to re-use that information not to mention the speed. For example, here’s a snippet of one of the StateCode enum available in the class.

public enum StateCode

{

    /// <summary>Alabama</summary>

    AL,

    /// <summary>Alaska</summary>

    AK,

    /// <summary>American Samoa</summary>

    AS,

    //,… Bunch of other states …,

    /// <summary>Wyoming</summary>

    WY

}

And here’s a snippet of the corresponding State enum.

public enum State

{

    /// <summary>AL</summary>

    Alabama,

    /// <summary>AK (Home Sweet Home)</summary>

    Alaska,

    /// <summary>AS</summary>

    American_Samoa,

 

    ///,… Bunch of other states…,

 

    /// <summary>WY</summary>

    Wyoming

}

The main AddressInfo class is used to hold an address, but isn’t all that useful nor interesting. The interesting methods are the static methods used to convert from state codes to states and vice versa. Here’s a couple examples of how you might use these methods:

// Get the state name based on the state code.

string stateName = AddressInfo.GetState(StateCode.AK);

Console.WriteLine(stateName); // Prints “Alaska”

 

// Get the state name based on the state code string

string stateCodeText = “CA”;

StateCode stateCode = AddressInfo.ParseStateCode(stateCodeText);

State state = AddressInfo.Convert(stateCode);

Console.WriteLine(state.ToString()); // Prints “California”

Let me know if you actually find this useful. The class itself can be downloaded here.

[Listening to: Psychedeliasmith / Give Me My Auger Back - Fat Boy Slim - On The Floor At The Boutique (4:21)]

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

2 responses

  1. Avatar for Kevin Peter
    Kevin Peter April 28th, 2005

    Thanks very much for the AddressInfo code. I am in the process of writing a class almost exactly like that, and in the middle of writing it, I wondered if someone had already written some state code/state name enumerations and conversions. Sure enough, Google led me to this page.



    This will save me a bunch of time. You have my gratitude.



    If anyone has trouble finding the link to download the code, the link can be found by clicking on the word "here" in the sentence "The class itself can be downloaded here.". In my browser the link was the same color as the rest of the text, so it took me a while to figure out where to download the file.

  2. Avatar for Joel Rumerman
    Joel Rumerman August 13th, 2008

    Hey Phil,
    Thanks for creating this code block for us! I'm starting to use it and I believe there's a bug in the ParseState method. Shouldn't the
    return (State)Enum.Parse(typeof(State), state.Replace(" ", ""), true);
    read
    return (State)Enum.Parse(typeof(State), state.Replace(" ", "_"), true);
    ?? The enumeration has underscores as the word separators. So District of Columbia becomes District_of_Columbia instead of DistrictOfColumbia.
    Thx, Joel