Click here to Skip to main content
15,888,610 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Switch boolean.... (reinventing if, unnecessarily) Pin
Member 1159491426-Jun-15 13:15
Member 1159491426-Jun-15 13:15 
GeneralRe: Switch boolean.... (reinventing if, unnecessarily) Pin
Al Chak27-Jun-15 22:01
Al Chak27-Jun-15 22:01 
GeneralRe: Switch boolean.... (reinventing if, unnecessarily) Pin
Richard Deeming29-Jun-15 2:25
mveRichard Deeming29-Jun-15 2:25 
GeneralRe: Switch boolean.... (reinventing if, unnecessarily) Pin
chaosworrier_oz29-Jun-15 17:55
chaosworrier_oz29-Jun-15 17:55 
GeneralRe: Switch boolean.... (reinventing if, unnecessarily) Pin
TheGreatAndPowerfulOz30-Jun-15 9:13
TheGreatAndPowerfulOz30-Jun-15 9:13 
GeneralRe: Switch boolean.... (reinventing if, unnecessarily) Pin
Jörgen Sigvardsson1-Jul-15 3:44
Jörgen Sigvardsson1-Jul-15 3:44 
GeneralRe: Switch boolean.... (reinventing if, unnecessarily) Pin
Bernhard Hiller1-Jul-15 8:06
Bernhard Hiller1-Jul-15 8:06 
JokeFun with Generic Enum Constraint PinPopular
Bernhard Hiller9-Jun-15 22:11
Bernhard Hiller9-Jun-15 22:11 
Ever tried to create a generic class which accepts Enum types only? Can be fun!
I have a non-generic class EnumDescConverter and wanted to add a generic version EnumDescConverter<T>. Easy, isn't it? Just write
C#
public class EnumDescConverter<T> : EnumDescConverter where T : Enum

and the error is
Constraint cannot be special class 'System.Enum'

Well, you have to use a different approach. An Enum is a struct which implements IConvertible. Hence you can do
C#
public class EnumDescConverter<T> : EnumDescConverter where T : struct, IConvertible

and then check in the constructor that T is really an enum: if (!enumType.IsEnum) { throw ...}

And now let's do more bad things. Poke tongue | ;-P
The non-generic class has a function
C#
public Enum GetEnumValue(string description)

Let's add a generic version in the generic class so that we do not need to cast the return value to the type of the enum in use:
C#
public T GetEnumValue(string description)

And the warning is:
warning CS0108: 'EnumDescConverter<T>.GetEnumValue(string)' hides inherited member 'EnumDescConverter.GetEnumValue(string)'. Use the new keyword if hiding was intended.

A possible solution, but not nice.
If you make the method in the base class virtual, and override it in the generic class, the error is:
'EnumDescConverter<T>.GetEnumValue(string)': return type must be 'System.Enum' to match overridden member 'EnumDescConverter.GetEnumValue(string)'

Hm. Let me change the name of the function, and in its body just call the base class' method and cast the return value (not a nice solution either):
C#
public T GetEnumValueT(string description)
{
   return (T)GetEnumValue(description);
}

And the next error arises:
Cannot convert type 'System.Enum' to 'T'

Of course,
C#
return GetEnumValue(description) as T;

doesn't work either because an Enum is not a class:
The type parameter 'T' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint

But the problem can be solved by a double-cast:
C#
return (T)((object)GetEnumValue(description));

That's Weird and Wonderful, isn't it? Big Grin | :-D
GeneralRe: Fun with Generic Enum Constraint Pin
Daniel Pfeffer9-Jun-15 22:29
professionalDaniel Pfeffer9-Jun-15 22:29 
GeneralRe: Fun with Generic Enum Constraint Pin
Agent__0079-Jun-15 22:31
professionalAgent__0079-Jun-15 22:31 
GeneralRe: Fun with Generic Enum Constraint Pin
Brisingr Aerowing10-Jun-15 8:50
professionalBrisingr Aerowing10-Jun-15 8:50 
GeneralRe: Fun with Generic Enum Constraint Pin
Bernhard Hiller12-Jun-15 4:35
Bernhard Hiller12-Jun-15 4:35 
GeneralRe: Fun with Generic Enum Constraint Pin
Brisingr Aerowing12-Jun-15 11:31
professionalBrisingr Aerowing12-Jun-15 11:31 
GeneralRe: Fun with Generic Enum Constraint Pin
PIEBALDconsult17-Jun-15 5:00
mvePIEBALDconsult17-Jun-15 5:00 
GeneralRe: Fun with Generic Enum Constraint Pin
Richard Deeming17-Jun-15 5:45
mveRichard Deeming17-Jun-15 5:45 
GeneralRe: Fun with Generic Enum Constraint Pin
PIEBALDconsult17-Jun-15 5:56
mvePIEBALDconsult17-Jun-15 5:56 
GeneralRe: Fun with Generic Enum Constraint Pin
greldak25-Jun-15 21:58
greldak25-Jun-15 21:58 
GeneralRe: Fun with Generic Enum Constraint Pin
James Curran1-Jul-15 5:19
James Curran1-Jul-15 5:19 
GeneralWPF ValidationErrors Pin
Bernhard Hiller29-May-15 4:28
Bernhard Hiller29-May-15 4:28 
GeneralRe: WPF ValidationErrors Pin
Brisingr Aerowing29-May-15 5:11
professionalBrisingr Aerowing29-May-15 5:11 
JokeMessage Closed Pin
29-May-15 5:14
User 113800029-May-15 5:14 
GeneralRe: WPF ValidationErrors Pin
Bernhard Hiller29-May-15 5:35
Bernhard Hiller29-May-15 5:35 
GeneralRe: WPF ValidationErrors Pin
Eddy Vluggen29-May-15 5:46
professionalEddy Vluggen29-May-15 5:46 
GeneralRe: WPF ValidationErrors Pin
Brisingr Aerowing29-May-15 6:00
professionalBrisingr Aerowing29-May-15 6:00 
GeneralRe: WPF ValidationErrors Pin
Eddy Vluggen29-May-15 5:43
professionalEddy Vluggen29-May-15 5:43 

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.