Override enum.ToString
-
Oh, Bernie's MCC (most competent cow-orker) Kevin is so great...
In the code base, there is a utility for converting enums to well readable, user-friendly, strings. Starting from the Description attribute, the utility was later enhanced to use localized resources. And it can do the conversion the other way round, too. It works well.
But it's too complicated. You must explicitly call EnumDescriptionConverter.GetEnumDescription(myEnumValue);. No, that's really bad, Bernie. Don't you know how to do it? You override ToString of the enum! You write an extension for that. Kevin asserted in an almost shouting voice, i.e. he was communicating in his normal style.
Well, with C#, ... There's no place in an enum definition where you could write public override string ToString(). But you could create an extension method (but didn't Kevin first shout, ehm, speak of "overriding"?):
public static class EnumExtension { public static string ToString(this Enum _e) { // a lot of code here } }
It compiles. But what happens when you try to use it? Bernie checked that.
[TestClass] public class EnumExtensionTests { [TestMethod] public void ToStringTest() { const string expected = ...; const LocalizedExample value1 = LocalizedExample.X1; string result = value1.ToString(); Assert.AreEqual(expected, result); } }
And then the test fails, and voilà, ToString obviously used the "normal" way of returning just the name of the value.
Of course, EnumExtension.ToString(myEnumValue) works. How is that different from the original?
If only Kevin was competent enough to read StackOverflow - he could read there that overriding enum.ToString is impossible.
-
@berniethebernie Enums in C# are special. They also have a lot of quirks when used in templates. (You can tell C# the templated value must be an
Enum
, but you can't tell it whichEnum
. Actually I think a recent update fixed that. But still bit me in the ass for years.)
-
@berniethebernie said in Override enum.ToString:
But what happens when you try to use it? Bernie checked that.
But I guess Kevin did not?
-
@blakeyrat said in Override enum.ToString:
@berniethebernie Enums in C# are special. They also have a lot of quirks when used in templates. (You can tell C# the templated value must be an
Enum
, but you can't tell it whichEnum
. Actually I think a recent update fixed that. But still bit me in the ass for years.)
-
@blakeyrat said in Override enum.ToString:
Enums in C# are special. They also have a lot of quirks when used in templates.
AFAIK C# doesn't have templates?
-
@gąska Generics. Whatever. I'm not Mr. Super Programming Nerd.
-
@blakeyrat it's a pretty big difference. For example, you don't need generic's source code to use it. Also, in most languages, generics can only be parametrized with type, while C++ templates allow integers (which includes enums). So it's only natural you can't use enums in generic parameters - they're not an interface, so no types could be used with such generic.
-
@gąska not just you don't need the source, they're a runtime construct instead of a compile-time construct. You can generate new generic signatures through reflection, even.
-
@pie_flavor they're definitely a compile time construct - compilation gets affected by them. Reflection is just a bonus.
-
@berniethebernie said in Override enum.ToString:
If only Kevin was competent enough to read StackOverflow - he could read there that overriding enum.ToString is impossible.
Well, TIL.
I have some code to remove/change now...
-
@dreikin pics or didn't happen.
-
@gąska said in Override enum.ToString:
@dreikin pics or didn't happen.
-
@dreikin Are you sure that it really does what you think it does?
Edit: unless you weren't sarcastic and literally mean that you have lots of code to fix and you're about to get going.
-
@dreikin Could be worse. You’re not trying to change the result for known values, just add the range check.
-
At first I thought I was in Coding Help and I was going to say "why would you do that?"
-
@gąska said in Override enum.ToString:
@dreikin Are you sure that it really does what you think it does?
Edit: unless you weren't sarcastic and literally mean that you have lots of code to fix and you're about to get going.
The second, though not really "lots" and I'd already done it (I hadn't pulled down the newer code on my laptop where I took that screenshot).
-
@unperverted-vixen said in Override enum.ToString:
@dreikin Could be worse. You’re not trying to change the result for known values, just add the range check.
And make sure that they go back to the same string as the 3rd-party source uses. Haven't needed to do that yet, so technically the code is unused right now; I just put them in to reverse the input To{Enum} functions.
-
@boomzilla said in Override enum.ToString:
But I guess Kevin did not?
Ha ha ha ha How much competence would that require?
-
Kevin is a dumb fuck.
C++ is open source, so he can change the code himself. Make an enum tostring method, put it into the C++ source code, and compile. Roll your own instead of sucking Micro$oft off!
-
@lorne-kates said in Override enum.ToString:
Kevin is a dumb fuck.
C++ is open source, so he can change the code himself. Make an enum tostring method, put it into the C++ source code, and compile. Roll your own instead of sucking Micro$oft off!
Pleeease don't give people like that ideas like that. :P
-
@heterodox said in Override enum.ToString:
Pleeease don't give people like that ideas like that
Yeah, Bernie fears that Kevin might exactly feel competent to do so. Sure he can do things better than the guys at Microsoft, and he has much more knowledge and experience. Of course.
-
@dreikin You added an Extension Method for a specific enumeration, not generically. Didn't try that, but would expect that to behave the same way.
Also, it returns the name of the enum value only - which is the default behavior of ToString with enums. That is, the extension method is totally useless, and you can't even test it, because from the result, you cannot decide if it was invoked at all.
-
@berniethebernie said in Override enum.ToString:
@dreikin You added an Extension Method for a specific enumeration, not generically. Didn't try that, but would expect that to behave the same way.
I tested it after seeing your OP and it does.
Also, it returns the name of the enum value only - which is the default behavior of ToString with enums. That is, the extension method is totally useless, and you can't even test it, because from the result, you cannot decide if it was invoked at all.
Not quite - some of the strings use underscores, but none of the enums do (although the example extension method set doesn't show that; my bad).
-
@dreikin Sounds like a good opportunity to consider introducing unit tests?