Click here to Skip to main content

Clever Code

RSS Feed RSS feed

Showcase your best code, your most elegant solution, or your most hard fought subtle bug you've found and fixed. Please, do not post programming questions in this forum - they will be removed.

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page  Show 
  Refresh
GeneralRe: The magicians big secret PinmemberAndrew Rissing9:21 20 Nov '09  
GeneralRe: The magicians big secret PinmemberYusuf9:59 20 Nov '09  
GeneralRe: The magicians big secret Pinmemberbquick7:53 27 Feb '10  
GeneralRe: The magicians big secret Pinmemberradioman.lt8:17 26 Feb '10  
GeneralDifferences between strncpy and strncpy_s PinmemberIndivara18:43 14 Nov '09  
GeneralRe: Differences between strncpy and strncpy_s Pinmembermegaadam4:57 25 Nov '09  
GeneralEnumerators PinPopularmvpJohn Simmons / outlaw programmer2:48 14 Nov '09  
In the spirit of the new charter of this forum - "wicked code" - I see this as wicked code...
 
I started programming long before many of the rest of you. Over the years, I've grown to have a huge distrust of not only users and their antics, but also other programmers and their seeming apathy regarding ensuring that data is valid before trying to use it. One of the problems I encountered was storing and retrieving enumerator values in data sources, and preparing the code to gracefully handle manually values - either modified by the user, or incorrectly set or interpreted by the programmer. So I came up with this method that I have include i pretty much every program I write:
 
		public static T IntToEnum<T>(int value, T defaultValue)
		{
			T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)(object)value : defaultValue;
			return enumValue;
		}
 
The purpose of the method is to allow the programmer to initialize a data member of a specified enumerator type to a value contained in the ordinal list. The problem this method addresses is that if the programmer retrieves an enum ordinal value as an int type, and wants to initialize an enum data member, he really has no programmatic idea if the value represents a valid ordinal. He simply tries to set it, and hope for the best (handling an exception if the assignment goes sideways on him).
 
This method allows the programmer to make the same attempt, but with controlled results and thereby avoiding the inevitable exception generated when an invalid ordinal value is used. Usage goes something like this:
 
enum SomeEnum { Zero=0, Five=5, Six=6, Eight=8 };
 
// this will result in the correct expected value - SomeEnum.Five
SomeEnum value = IntToEnum(5, SomeEnum.Zero);
 
// this will result in SomeEnum.Zero because the value (4) isn't a valid ordinal in the enumerator
value = IntToEnum(4, SomeEnum.Zero);

 
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

GeneralRe: Enumerators PinmemberRichard MacCutchan3:21 14 Nov '09  
GeneralRe: Enumerators PinmvpJohn Simmons / outlaw programmer2:39 15 Nov '09  
GeneralRe: Enumerators PinmemberRichard MacCutchan2:47 15 Nov '09  
JokeRe: Enumerators PinmemberChris Meech4:40 16 Nov '09  
GeneralRe: Enumerators PinmemberRichard MacCutchan5:12 16 Nov '09  
GeneralRe: Enumerators PinmemberAndrew Rissing6:12 16 Nov '09  
GeneralRe: Enumerators PinmemberPIEBALDconsult7:36 16 Nov '09  
GeneralRe: Enumerators PinmemberAndrew Rissing9:36 16 Nov '09  
GeneralRe: Enumerators PinmemberPIEBALDconsult10:03 16 Nov '09  
GeneralRe: Enumerators Pinmemberlongnights10:12 20 Nov '09  
GeneralRe: Enumerators PinmemberPIEBALDconsult13:30 20 Nov '09  
GeneralRe: Enumerators PinmemberGraham Parkings1:45 20 Nov '09  
GeneralRe: Enumerators PinmvpJohn Simmons / outlaw programmer2:37 20 Nov '09  
GeneralRe: Enumerators PinmemberPIEBALDconsult15:22 20 Nov '09  
GeneralRe: Enumerators PinmvpJohn Simmons / outlaw programmer1:52 21 Nov '09  
GeneralRe: Enumerators PinmemberPIEBALDconsult3:13 21 Nov '09  
GeneralRe: Enumerators [modified] PinmvpJohn Simmons / outlaw programmer4:03 21 Nov '09  
GeneralRe: Enumerators PinmvpJohn Simmons / outlaw programmer1:53 21 Nov '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 11 Feb 2012
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid