Click here to Skip to main content
14,641,113 members
Home » Discussions » C#
   

C#

 
GeneralRe: Refreshing data on a parent form at child close Pin
OriginalGriff24-Apr-20 20:08
mveOriginalGriff24-Apr-20 20:08 
GeneralRe: Refreshing data on a parent form at child close Pin
Uranium-23525-Apr-20 19:42
MemberUranium-23525-Apr-20 19:42 
AnswerRe: Refreshing data on a parent form at child close Pin
#realJSOP22-Apr-20 7:15
mva#realJSOP22-Apr-20 7:15 
QuestionMaintain the Runing balance in Datagridview Pin
Mukhtar Ashiq21-Apr-20 9:04
MemberMukhtar 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
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 
Those type constraints will allow things other than Enums though, which will cause an ArgumentException at runtime:
int x = typeof(int).ToEnum<int>("Friday");
// ArgumentException: Type provided must be an Enum.
If you're using a relatively recent compiler / language version (C# 7.3 or later), you can use an Enum type constraint if you want to be absolutely sure the type parameter is an Enum:
public static TEnum ToEnum<TEnum>(this Type tenum, string str, bool ignoreCase = false) where TEnum : struct, Enum { ... }
...

WeekDays friday = typeof(WeekDays).ToEnum<WeekDays>("friday");
// Works as expected.

int x = typeof(int).ToEnum<int>("Friday");
// Compiler error: CS0315 
// The type 'int' cannot be used as type parameter 'TEnum' in the generic type or method 'ToEnum<TEnum>(Type, string, bool)'. 
// There is no boxing conversion from 'int' to 'System.Enum'.
(This was supported in the CLR since generics were introduced in v2, but it was only added to C# in 2018.)

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.

For example, what is the expected output from the following (admittedly terrible) code?
enum A { Zero = 0, One = 1, Two = 2, Default = One }
enum B { Zero = 1, One = 2, Two = 4, Default = Two }

B x = typeof(A).ToEnum<B>("Default");
B y = typeof(A).ToEnum<B>("Two");
If it's using typeof(TEnum) and ignoring the tenum parameter, the answer will be Two and Two.

If it's using the tenum parameter, the answer will be Zero and One.

Enum constraints | C# Online Compiler | .NET Fiddle[^]


At the moment, I can only think of two alternatives:
public static class EnumHelper
{
    public static TEnum ToEnum<TEnum>(string str, bool ignoreCase) where TEnum : struct, Enum { ... }
}
...
WeekDays friday = EnumHelper.ToEnum<WeekDays>("friday");
Or:
public static TEnum ToEnum<TEnum>(this TEnum ignored, string str, bool ignoreCase) where TEnum : struct, Enum { ... }
...
WeekDays friday = WeekDays.Monday.ToEnum("friday");
I have a slight preference for option 1.



"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
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 
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 

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.