Convert enclosing type - implicit enum conversion
Trying to get Environment.Exit(enum) to accept an enum without explicitly casting the parameter
The code below works one-way, but not the other and I'm unsure as to why
I would love to know if anyone has a solution to get this working, I don't want to haver to explicitly cast them every time I make a call using the enum (it will be referenced elsewhere as well
8 Replies
i assume ExitCodes is not an actual
enum
enum? because you can't define an implicit conversion for an enumIt is an actual enum, yes
Well, enum : int, anyway
well, then i don't know what you mean by "the code below works" because it does not
there is nowhere to put an implicit operator inside of an enum
I had it semi-working one-way conversion, but not the other
Though I can't get back to that point, so maybe I'm an idiot, often am.
if you want something resembling that you will have to make a struct which mimics an enum
Basically for readability (and spitting out enum.tostring in debug log) I just want to be able to pass a named string into Environ.Exit, and other funcs like that
ps; sorry for terrible typing, stroke couple weeks back rendered left hand mostly limp, so re-learning how to type
ToString which contains the name would admittedly be a little harder
here's something i like better:
then
ExitCode.Ok.ToString
is
ExitCode { Value = 0, Name = Ok }the name gets picked up automatically in this case and if you want to hide
Value
from ToString you can make it private
i commented out the one cast because it's hard to make (ExitCode)0
return Name = "Ok"
i mean. it's not that hard. it would just make the code either ugly or harder to maintain or both
if you don't need it (i do not see why you would need it, at least not for the thing you asked about) then that will work greatDinner atm, but I'm excited AF to go over all that you wrote, thank you!