While it’s still a few months away from the conference, tickets are already flying out the door. Don't forget that we have limited seating and once registration closes, tickets will no longer be available. At $100 for 3 great days of technical training, in-person networking, open spaces and lots of fun, DEVLINK 2010 is truly the best deal going! That's less than $5 per hour for some of the most relevant and important training you can receive all year!

So, be sure to get your ticket before they're all gone and join us August 5 - 7, 2010 for DEVLINK 2010!

Announcing the DEVLINK iPhone App!

The iPhone store seemingly has an app for everything. Need an app to calculate the tip for a group of 45 at a restaurant? Yep, at least 100 apps for that. Well now, there's an iPhone App for DEVLINK 2010!

The DEVLINK 2010 iPhone app brings everything you need to determine which sessions to attend, what speakers will be attending and what's going on at DEVLINK 2010 to your iPhone. You can find out more about our great sponsors, check out the latest tweets about DEVLINK 2010 and even check out a map of Nashville to figure out what you want to do when you're not at DEVLINK!

The app was developed by John Baker for the conference, and thanks to John's generosity, you can get this app from the App Store for the amazing low price of... FREE!

Even if you don't have an iPhone, you can still get the lowdown on all sessions for DEVLINK 2010 at our web site. The entire session list is color coded based on track, allowing you to find what you're most interested in and follow that track easily. You can also filter the list down to show only the tracks, timeslots or speakers you are interested in.

Additionally, information about our great speakers is now available at the DEVLINK 2010 website. Click on any of the speaker's faces to get more information about him/her, visit his/her web site and even check out his/her Twitter page.

Visual Studio 2010 launches TODAY!!!! and to celebrate the we are having a Visual Studio 2010 Install Fest and Board Game Geekout on Saturday April 17, 2010. That’s this weekend!!!! We will be playing all kinds of games including Xbox, PS3, Wii for console games and Settlers of Catan and D&D for board games.

So, I was dredging around in a some old code I inherited looking for that one magical place to make my new change when I came across a piece of code that, well, just made me laugh and then cry.

Here it is:

1: switch (ddlRollupInterval.SelectedValue)

2: {

3: case"Daily":

4: r.RollupInterval = Report.Interval.Daily;

5: break;

6: case"Weekly":

7: r.RollupInterval = Report.Interval.Weekly;

8: break;

9: case"Monthly":

10: r.RollupInterval = Report.Interval.Monthly;

11: break;

12: default:

13: r.RollupInterval = Report.Interval.Yearly;

14: break;

15: }

Looking at the code it is very obvious what it is doing, it is setting an enum value based on the string value of a dropdown. But did it really take that much code to do it. Well of course not.

An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable. For example, assume that you have to define a variable whose value will represent a report interval. There are only 4 meaningful values which that variable will ever store. To define those values, you can use an enumeration type, which is declared by using the enum keyword.

1: enum ReportInterval

2: {

3: Daily,

4: Weekly,

5: Monthly,

6: Yearly

7: }

The above enum was declared and then an instance was used on the Report object.

Enums are extremely flexible and can be used in a number of ways. Lets look at a few ways you can use enums.

Get the Names from the Enum

Lets take the ReportInteval enum we created above and look how we would get an Array of strings that represent the names of each valid enum.

1: var enumNames = Enum.GetNames(typeof (ReportInterval));

This would allow us to populate that ddlRollupInterval dropdown with all the possible values from the enum

Ok, so now we have a dropdown filled with the names from the enum, how do we clean up that code we found. You remember that ugly switch statement that started this whole thing. Once a user selects something in the dropdown and clicks save we want to set the enum based on the string that was selected right? Lets look at how to do that next.

Likewise you can also get the values with the following code:

1: Array enumValues = Enum.GetValues(typeof(ReportInterval));

Get an Enum from a String

This too is much easier than one would think. Using the Enum.Parse() method provides a quick way to convert from a string to an instance of the desired Enum.

This works great in our case because we filled the dropdown with only valid values. But what if you weren’t sure if the value was valid for the enum or not. You know those pesky users sometimes give us data that doesn’t fit our model. ;-) You can use the Enum.IsDefined() which returns an indication letting us know if a constant with the specified value exists in a specified enumeration.

If our user was able to add a value, say ‘Quarterly’ to our dropdown by some other part of the application and then selected it we would want to test the string we were provided to make sure we don’t throw an unwanted exception.

1: var ourUserInputString = "Quarterly"

2:

3: if (Enum.IsDefined(typeof (ReportInterval), outUserInputString)

4: {

5: // It is ok go ahead and pase the enum

6: }

7: else

8: {

9: // Alert the user an invalid string was selected.

10: }

Get an Enum from an Integer

Getting an enum instance form an integer is even easier just cast the integer to the desired Enum.

1: ReportInterval interval = (ReportInterval) testInt;

Of course we would never let our users add values to the dropdown because we want to constrain the valid values to be one that is on the enum, but I wanted to show you that it can be done.

Limit Method Call Arguments

Another usage for an enum is to limit the values a developer can pass to a method on your object. Using our ReportInterval enum as an argument to a LoadReport method on our Report call gives us the safety net to know we are only going to get a valid value. Where as if we just used a string we could get a value that doesn’t make sense.

1: var status = RunReport(ReportInterval.Daily);

Another great part of this is a that Visual Studio will start complaining that the value is in valid unless it is from the enum, further more the compiler will an argument '1': cannot convert from 'string' to 'EnumTest.ReportInterval' exception. I don’t know about your but I like that.

Summary

So what was the final result of fixing the code smell you say? I replaced that huge 15 line switch statement with the two lines of code, one to verify that the value was correct, remember this is a very, very legacy application and I can not trust that the values were actually loaded to the dropdown from the enum, and one to set the ReportInterval if it checked out good.

I hope that this has shown you just a few ways that the simple Enum can enhance you programming life.

Episode 4 of User Group Radio has been released. While at the MVP Summit in February I got the chance to set down with Rob Zelt the President of INETA North America. Rob talks about INETA’s mission, programs, and offers information for User Group Leaders on how they can benefit from being an INETA User Group.

This was a fun interview and Rob talked at length about the role of INETA and how it impacts community.