Click here to Skip to main content
15,906,816 members
Home / Discussions / C#
   

C#

 
AnswerRe: Refreshing data on a parent form at child close Pin
#realJSOP22-Apr-20 7:15
professional#realJSOP22-Apr-20 7:15 
QuestionMaintain the Runing balance in Datagridview Pin
Mukhtar Ashiq21-Apr-20 9:04
Mukhtar Ashiq21-Apr-20 9:04 
SuggestionRe: Maintain the Runing balance in Datagridview Pin
Richard MacCutchan21-Apr-20 9:15
mveRichard MacCutchan21-Apr-20 9:15 
AnswerRe: Maintain the Runing balance in Datagridview Pin
ZurdoDev21-Apr-20 9:51
professionalZurdoDev21-Apr-20 9:51 
Questionextending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 8:58
professionalBillWoodruff20-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
professionalBillWoodruff20-Apr-20 11:56 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff20-Apr-20 22:10
professionalBillWoodruff20-Apr-20 22:10 
@RichardDeeming ... Hi Richard, fyi: in the code I posted on QA [^] I do check to make sure the Type is 'Enum.:
    // Enum Type, string, optional bool => Enum value
public static TEnum ToEnum<TEnum>(this Type tenum, string str, bool ignorecase = false)
    where TEnum : struct, IConvertible
{
    if (! tenum.IsEnum)
        throw new ArgumentException("TEnum must be an Enum type");

    TEnum result;

    // note ignore case parameter
    if (Enum.TryParse<TEnum>(str, ignorecase, out result))
    {
        return result;
    }

    throw new ArgumentException($"{str} is not a valid string for conversion to {tenum.FullName}");
}
Richard Deeming wrote:
However, I'm not too keen on having to pass the enum type twice. It's not clear from the signature which type will be used - the generic type parameter, or the tenum argument.
I'm uncomfortable with that syntax as well.

To make that more bomb-proof:
// Enum Type, string, optional bool => Enum value
public static TEnum ToEnum<TEnum>(this Type tenum, string str, bool ignorecase = false)
    where TEnum : struct, IConvertible
{
    Type returntype = typeof(TEnum);

    if (!returntype.IsEnum)
        throw new ArgumentException("Return Type must be an Enum type");

    if (!tenum.IsEnum)
        throw new ArgumentException("Type parameter must be an Enum type");

    if (tenum != returntype)
        throw new ArgumentException("Type and Return type must be the same");

    TEnum result;

    // note ignore case parameter
    if (Enum.TryParse<TEnum>(str, ignorecase, out result))
    {
        return result;
    }

    throw new ArgumentException($"{str} is not a valid string for conversion to {tenum.FullName}");
}
Ideally, we'd have a way for these edge cases to not even compile, but, ideally it would not be 104F (feels like 110F) in Thailand right now, and my air-con would not be wimping out on me Smile | :)
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali


modified 21-Apr-20 4:28am.

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
professionalBillWoodruff21-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 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 8:29
professionalBillWoodruff21-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
professionalBillWoodruff21-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
professionalBillWoodruff21-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
professionalBillWoodruff21-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
professionalBillWoodruff22-Apr-20 6:12 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff21-Apr-20 21:53
professionalBillWoodruff21-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
professionalBillWoodruff22-Apr-20 3:01 
AnswerRe: extending an Enum with a function that returns a generic value ? Pin
#realJSOP22-Apr-20 7:25
professional#realJSOP22-Apr-20 7:25 
GeneralRe: extending an Enum with a function that returns a generic value ? Pin
BillWoodruff22-Apr-20 7:39
professionalBillWoodruff22-Apr-20 7:39 

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.