So I have this struct:
public struct PurchaseOrderStatus {
public const string Open = "Open", Received = "Received";
}
How do I convert if I have the following:
string status = "Open";
To:
PurchaseOrderStatus.Open;
By Convert, I mean, how do I do this:
PurchaseOrderStatus POS;
String status = "Open";
POS = status;
I would suggest using “smart enums” here:
You can store arbitrary information here, including text which wouldn’t be valid identifiers (so long as it doesn’t change, of course). It’s still strongly typed (unlike your strings) and you can give other behaviour to it. You can even create subclasses if you remove the
sealedmodifier and add the subclasses as nested classes so they still have access to the private constructor.Oh, and there’s a genuinely limited set of values (the ones you’ve defined and
null) unlike with regular enums.The only downside is that you can’t switch on it.