Click here to Skip to main content
15,850,445 members
Home / Discussions / C#
   

C#

 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis21-Apr-20 10:16
sysadminMatthew Dennis21-Apr-20 10:16 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 21:57
professionalBillWoodruff21-Apr-20 21:57 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis22-Apr-20 5:35
sysadminMatthew Dennis22-Apr-20 5:35 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 7:12
professionalBillWoodruff22-Apr-20 7:12 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 22:53
professionalBillWoodruff21-Apr-20 22:53 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming22-Apr-20 0:25
mveRichard Deeming22-Apr-20 0:25 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 4:01
professionalBillWoodruff22-Apr-20 4:01 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP22-Apr-20 8:25
mve#realJSOP22-Apr-20 8:25 
I have a couple of static methods that attempt to establish the correct enum value from the specified string (or int). I use them when loading data from a source that could potentially be corrupted by manual modifications by fat-fingered idiot users.

C#
 public static T IntToEnum<T>(int value, T defaultValue)
 {
     T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)(object)value : defaultValue;
     return enumValue;
 }

 public static T StringToEnum<T>(string value, T defaultValue)
 {
     T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)Enum.Parse(typeof(T), value) : defaultValue;
     return enumValue;
}

".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 8:39
professionalBillWoodruff22-Apr-20 8:39 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP22-Apr-20 8:47
mve#realJSOP22-Apr-20 8:47 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 21:31
professionalBillWoodruff22-Apr-20 21:31 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP23-Apr-20 1:58
mve#realJSOP23-Apr-20 1:58 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff23-Apr-20 6:25
professionalBillWoodruff23-Apr-20 6:25 
QuestionSas.dll Pin
Member 1480415820-Apr-20 5:26
Member 1480415820-Apr-20 5:26 
AnswerRe: Sas.dll Pin
Richard Andrew x6420-Apr-20 5:49
professionalRichard Andrew x6420-Apr-20 5:49 
GeneralRe: Sas.dll Pin
OriginalGriff20-Apr-20 5:51
mveOriginalGriff20-Apr-20 5:51 
AnswerRe: Sas.dll Pin
OriginalGriff20-Apr-20 5:50
mveOriginalGriff20-Apr-20 5:50 
QuestionAccess WebApi remotely - C'mon guys I don't believe no one has a clue ! any hint will do Pin
pkfox20-Apr-20 4:21
professionalpkfox20-Apr-20 4:21 
AnswerRe: Access WebApi remotely - C'mon guys I don't believe no one has a clue ! any hint will do Pin
Peter Shaw22-Apr-20 11:22
professionalPeter Shaw22-Apr-20 11:22 
GeneralRe: Access WebApi remotely - C'mon guys I don't believe no one has a clue ! any hint will do Pin
pkfox22-Apr-20 13:05
professionalpkfox22-Apr-20 13:05 
GeneralRe: Access WebApi remotely - C'mon guys I don't believe no one has a clue ! any hint will do Pin
Peter Shaw22-Apr-20 13:12
professionalPeter Shaw22-Apr-20 13:12 
GeneralRe: Access WebApi remotely - C'mon guys I don't believe no one has a clue ! any hint will do Pin
pkfox22-Apr-20 13:16
professionalpkfox22-Apr-20 13:16 
GeneralRe: Access WebApi remotely - C'mon guys I don't believe no one has a clue ! any hint will do Pin
Peter Shaw22-Apr-20 13:25
professionalPeter Shaw22-Apr-20 13:25 
GeneralRe: Access WebApi remotely - C'mon guys I don't believe no one has a clue ! any hint will do Pin
pkfox22-Apr-20 13:29
professionalpkfox22-Apr-20 13:29 
GeneralRe: Access WebApi remotely - C'mon guys I don't believe no one has a clue ! any hint will do Pin
Peter Shaw22-Apr-20 13:45
professionalPeter Shaw22-Apr-20 13: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.