Click here to Skip to main content
14,635,091 members
Home » Discussions » C#
   

C#

 
AnswerRe: Maintain the Runing balance in Datagridview Pin
ZurdoDev21-Apr-20 9:51
mveZurdoDev21-Apr-20 9:51 
Questionextending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 8:58
mveBillWoodruff20-Apr-20 8:58 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming20-Apr-20 9:34
mveRichard Deeming20-Apr-20 9:34 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 11:56
mveBillWoodruff20-Apr-20 11:56 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 22:10
mveBillWoodruff20-Apr-20 22:10 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 0:01
mveRichard Deeming21-Apr-20 0:01 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 4:30
mveBillWoodruff21-Apr-20 4:30 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 5:17
mveRichard Deeming21-Apr-20 5:17 
If you make the parameter TEnum, you won't need to check whether it's the same type as the type parameter, and you won't need to repeat the type when you call it.
public static Func<string, bool, TEnum> MakeConverter<TEnum>(this TEnum tenum) ...
var econverter = WeekDays.Default.MakeConverter();
I don't think you need to worry about getting an exception when you create the function - anything that's thrown then won't be recoverable anyway.

You can also let the compiler infer the parameter types for the returned Func<>.
public static Func<string, bool, TEnum> MakeConverter<TEnum>(this TEnum tenum)
    where TEnum : struct, Enum
{
    return (str, ignoreCase) =>
    {
        if (Enum.TryParse<TEnum>(str, ignoreCase, out var result)) return result;
        throw new ArgumentException($"'{str}' is not a valid string for conversion to {typeof(TEnum).FullName}");
    };
}
With a throw expression[^], the converter function can come down to a single line:
return (str, ignoreCase) => Enum.TryParse<TEnum>(str, ignoreCase, out var result) ? result
    : throw new ArgumentException($"'{str}' is not a valid string for conversion to {typeof(TEnum).FullName}");
You could even make the factory method an expression-bodied method, although I think that might be taking things a little too far:
public static Func<string, bool, TEnum> MakeConverter<TEnum>(this TEnum tenum) where TEnum : struct, Enum
   => (str, ignoreCase) => Enum.TryParse<TEnum>(str, ignoreCase, out var result) ? result
    : throw new ArgumentException($"'{str}' is not a valid string for conversion to {typeof(TEnum).FullName}");

NB: If you want to maintain the optional parameter, you can't use the generic Func<> delegate; you'd need a custom delegate type:
public delegate TEnum EnumConverterDelegate<TEnum>(string str, bool ignoreCase = false) where TEnum : struct, Enum;

public static EnumConverterDelegate<TEnum> MakeConverter<TEnum>(this TEnum value) where TEnum : struct, Enum
   => (str, ignoreCase) => Enum.TryParse<TEnum>(str, ignoreCase, out var result) ? result
    : throw new ArgumentException($"'{str}' is not a valid string for conversion to {typeof(TEnum).FullName}");




"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 8:29
mveBillWoodruff21-Apr-20 8:29 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis21-Apr-20 5:36
sysadminMatthew Dennis21-Apr-20 5:36 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 8:27
mveBillWoodruff21-Apr-20 8:27 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 8:37
mveRichard Deeming21-Apr-20 8:37 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 8:59
mveBillWoodruff21-Apr-20 8:59 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis21-Apr-20 9:16
sysadminMatthew Dennis21-Apr-20 9:16 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 20:57
mveBillWoodruff21-Apr-20 20:57 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Matthew Dennis22-Apr-20 4:35
sysadminMatthew Dennis22-Apr-20 4:35 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 6:12
mveBillWoodruff22-Apr-20 6:12 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 21:53
mveBillWoodruff21-Apr-20 21:53 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
Richard Deeming21-Apr-20 23:25
mveRichard Deeming21-Apr-20 23:25 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 3:01
mveBillWoodruff22-Apr-20 3:01 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP22-Apr-20 7:25
mva#realJSOP22-Apr-20 7:25 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 7:39
mveBillWoodruff22-Apr-20 7:39 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP22-Apr-20 7:47
mva#realJSOP22-Apr-20 7:47 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 20:31
mveBillWoodruff22-Apr-20 20:31 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP23-Apr-20 0:58
mva#realJSOP23-Apr-20 0:58 

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.