Enum is for Enumeration

Posted on: Fri Aug 01 16:08:24 -0700 2008. Updated on: Mon Sep 14 14:41:10 -0700 2009.
Category: DotNet

Enumerators can come in handy anyplace where you need a list of items that could be passed in as parameters. This way you avoid using a string or an integer that could get messed up and cause a run time error. Declared enums provide intellisense in VS so they are quick to code too.

An example would be user types or groups, ie Admin, public, editor, etc.
In many cases it is better to have types databound to sql database but for simpler things an enum will do.

public enum Groups
{
   Unknown = 0,
   admin = 1,
   editor = 2,
   everyone = 3
}

int admin = (int)Groups.admin;
// the above would return integer with value 1
Groups group = (Groups)2;
// the above would return enum Group object with "editor" option selected.
Note that I define the integer value of each item and start with Unknown = 0. There is a bug that causes an enum not to travel well through web services. So, if you are declaring an enum in a web service and want to access it on the client, be sure to start with 0.
To bind an enum to a dropdown box or other datasource, this guy has a good article that works: http://www.developerfusion.com/code/4713/binding-a-control-to-an-enum/

Comments:

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.