C
C#7mo ago
Sensato

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
public static implicit operator ExitCodes(ExitCodes value) {
return (int)value;
}
public static implicit operator ExitCodes(int value) {
return (ExitCodes)value;
}
public static implicit operator ExitCodes(ExitCodes value) {
return (int)value;
}
public static implicit operator ExitCodes(int value) {
return (ExitCodes)value;
}
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
reflectronic
reflectronic7mo ago
i assume ExitCodes is not an actual enum enum? because you can't define an implicit conversion for an enum
Sensato
SensatoOP7mo ago
It is an actual enum, yes Well, enum : int, anyway
reflectronic
reflectronic7mo ago
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
Sensato
SensatoOP7mo ago
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.
reflectronic
reflectronic7mo ago
if you want something resembling that you will have to make a struct which mimics an enum
Sensato
SensatoOP7mo ago
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
reflectronic
reflectronic7mo ago
public readonly record struct ExitCode(int value)
{
private int value = value;

public static ExitCode Ok => new(0);
public static ExitCode NotOk => new(1);

public static implicit operator int(ExitCode value) => value.value;

public static implicit operator ExitCode(int value) => new(value);
}
public readonly record struct ExitCode(int value)
{
private int value = value;

public static ExitCode Ok => new(0);
public static ExitCode NotOk => new(1);

public static implicit operator int(ExitCode value) => value.value;

public static implicit operator ExitCode(int value) => new(value);
}
ToString which contains the name would admittedly be a little harder here's something i like better:
public readonly record struct ExitCode
{
public int Value { get; }
public string Name { get; }

private ExitCode(int value, [CallerMemberName] string name = "")
{
Value = value;
Name = name;
}

public static ExitCode Ok { get; } = new(0);
public static ExitCode NotOk { get; } = new(1);

public static implicit operator int(ExitCode value) => value.Value;
// public static implicit operator ExitCode(int value) => new(value, "");
}
public readonly record struct ExitCode
{
public int Value { get; }
public string Name { get; }

private ExitCode(int value, [CallerMemberName] string name = "")
{
Value = value;
Name = name;
}

public static ExitCode Ok { get; } = new(0);
public static ExitCode NotOk { get; } = new(1);

public static implicit operator int(ExitCode value) => value.Value;
// public static implicit operator ExitCode(int value) => new(value, "");
}
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 great
Sensato
SensatoOP7mo ago
Dinner atm, but I'm excited AF to go over all that you wrote, thank you!
Want results from more Discord servers?
Add your server