Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
GeneralRe: Enum and sets Pin
Dirso10-Sep-08 3:42
Dirso10-Sep-08 3:42 
GeneralRe: Enum and sets Pin
Ennis Ray Lynch, Jr.10-Sep-08 3:46
Ennis Ray Lynch, Jr.10-Sep-08 3:46 
GeneralRe: Enum and sets Pin
J4amieC10-Sep-08 4:28
J4amieC10-Sep-08 4:28 
AnswerRe: Enum and sets Pin
Ennis Ray Lynch, Jr.10-Sep-08 3:21
Ennis Ray Lynch, Jr.10-Sep-08 3:21 
AnswerRe: Enum and sets Pin
PIEBALDconsult10-Sep-08 4:45
mvePIEBALDconsult10-Sep-08 4:45 
GeneralRe: Enum and sets Pin
Dirso12-Sep-08 4:31
Dirso12-Sep-08 4:31 
GeneralRe: Enum and sets Pin
PIEBALDconsult12-Sep-08 5:15
mvePIEBALDconsult12-Sep-08 5:15 
AnswerRe: Enum and sets Pin
#realJSOP10-Sep-08 4:51
mve#realJSOP10-Sep-08 4:51 
I store them as int, and the reconstitute them with a favorite little function of mine that verifies that the int value is valid inside the enum.

//--------------------------------------------------------------------------------
/// <summary>
/// Casts the specified integer to an appropriate enum. If all else fails, 
/// the enum will be returned as the specified default ordinal.
/// </summary>
/// <param name="value">The integer value representing an enumeration element</param>
/// <param name="defaultValue">The default enumertion to be used if the specified "value" 
does not exist in the enumeration definition</param>
/// <returns></returns>
public static T IntToEnum<T>(int value, T defaultValue)
{
	T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)(object)value : defaultValue;
	return enumValue;
}


In the examples posted by another user, ordinals of 1, 2, and 4 were used. If you called my function with an invalid value, it would gracefully recover by returning the specified default enum value. So...

ChangeOptions changeOption = IntToEnum(5, ChangeOptions.Orientation)


...would result in changeOption being set to ChangeOptions.Orientation (because 5 is an invalid ordinal), while doing this...

ChangeOptions changeOption = IntToEnum(2, ChangeOptions.Orientation)


...would result in changeOption being set to ChangeOptions.Size (because 2 is a valid ordinal).


"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." - Jason Jystad, 10/26/2001


QuestionPrint out a datagridview with the info Pin
Atis10-Sep-08 2:47
Atis10-Sep-08 2:47 
AnswerRe: Print out a datagridview with the info Pin
Giorgi Dalakishvili10-Sep-08 3:00
mentorGiorgi Dalakishvili10-Sep-08 3:00 
AnswerRe: Print out a datagridview with the info Pin
Manas Bhardwaj10-Sep-08 3:00
professionalManas Bhardwaj10-Sep-08 3:00 
Questionneed help with a code Pin
DanteV9210-Sep-08 2:17
DanteV9210-Sep-08 2:17 
AnswerRe: need help with a code Pin
leppie10-Sep-08 2:34
leppie10-Sep-08 2:34 
GeneralRe: need help with a code Pin
DanteV9210-Sep-08 3:02
DanteV9210-Sep-08 3:02 
GeneralRe: need help with a code Pin
blackjack215010-Sep-08 3:04
blackjack215010-Sep-08 3:04 
GeneralRe: need help with a code Pin
Alan Balkany10-Sep-08 3:45
Alan Balkany10-Sep-08 3:45 
GeneralRe: need help with a code Pin
DanteV9210-Sep-08 5:58
DanteV9210-Sep-08 5:58 
GeneralRe: need help with a code Pin
Alan Balkany10-Sep-08 5:59
Alan Balkany10-Sep-08 5:59 
GeneralRe: need help with a code Pin
DanteV9210-Sep-08 6:09
DanteV9210-Sep-08 6:09 
GeneralRe: need help with a code Pin
Alan Balkany10-Sep-08 6:11
Alan Balkany10-Sep-08 6:11 
GeneralRe: need help with a code Pin
DanteV9210-Sep-08 6:24
DanteV9210-Sep-08 6:24 
GeneralRe: need help with a code Pin
Alan Balkany10-Sep-08 10:50
Alan Balkany10-Sep-08 10:50 
AnswerRe: need help with a code Pin
Ennis Ray Lynch, Jr.10-Sep-08 3:19
Ennis Ray Lynch, Jr.10-Sep-08 3:19 
GeneralRe: need help with a code Pin
DanteV9210-Sep-08 3:38
DanteV9210-Sep-08 3:38 
GeneralRe: need help with a code Pin
Ennis Ray Lynch, Jr.10-Sep-08 3:45
Ennis Ray Lynch, Jr.10-Sep-08 3:45 

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

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