Click here to Skip to main content
15,790,440 members
Home / Discussions / C#
   

C#

 
QuestionMaintain the Runing balance in Datagridview Pin
Mukhtar Ashiq21-Apr-20 10:04
Mukhtar Ashiq21-Apr-20 10:04 
SuggestionRe: Maintain the Runing balance in Datagridview Pin
Richard MacCutchan21-Apr-20 10:15
mveRichard MacCutchan21-Apr-20 10:15 
AnswerRe: Maintain the Runing balance in Datagridview Pin
ZurdoDev21-Apr-20 10:51
professionalZurdoDev21-Apr-20 10:51 
Questionextending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 9:58
professionalBillWoodruff20-Apr-20 9:58 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming20-Apr-20 10:34
mveRichard Deeming20-Apr-20 10:34 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 12:56
professionalBillWoodruff20-Apr-20 12:56 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 23:10
professionalBillWoodruff20-Apr-20 23:10 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 1:01
mveRichard Deeming21-Apr-20 1:01 
How about:
C#
public static TEnum ToEnum<TEnum>(this Type tenum, string str, bool ignoreCase = false)
    where TEnum : struct, Enum
{
    #if DEBUG
    
    if (tenum != typeof(TEnum))
        throw new ArgumentException("Type {tenum.FullName} does not match return type {typeof(TEnum).FullName}");
    
    #endif
    
    if (Enum.TryParse<TEnum>(str, ignoreCase, out TEnum result))
    {
        return result;
    }
    
    throw new ArgumentException($"'{str}' is not a valid string for conversion to {typeof(TEnum).FullName}");
}
The Enum constraint means you don't need to check whether TEnum is an Enum type; anything else would be a compiler error.

The tenum parameter isn't really used, so it probably makes sense to omit the comparison in a release build.

And if we're going to throw an exception if tenum is not the same as typeof(TEnum), we don't really need to test whether tenum is an Enum type first. Smile | :)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 5:30
professionalBillWoodruff21-Apr-20 5:30 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 6:17
mveRichard Deeming21-Apr-20 6:17 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 9:29
professionalBillWoodruff21-Apr-20 9:29 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis21-Apr-20 6:36
sysadminMatthew Dennis21-Apr-20 6:36 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 9:27
professionalBillWoodruff21-Apr-20 9:27 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 9:37
mveRichard Deeming21-Apr-20 9:37 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 9:59
professionalBillWoodruff21-Apr-20 9:59 
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 
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 

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.