Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we use an empty string value for a SmartEnum #302

Closed
Tyler-V opened this issue Aug 17, 2022 · 6 comments · Fixed by #412
Closed

How can we use an empty string value for a SmartEnum #302

Tyler-V opened this issue Aug 17, 2022 · 6 comments · Fixed by #412

Comments

@Tyler-V
Copy link

Tyler-V commented Aug 17, 2022

We have several codes that can appear on a file; "V", "R", "S", "Z" or it is empty ""

When we add a smart enum property for "" we are unable to invoke TryFromName without an error

What is the proper way to handle a smart enum for a null or empty string value?

Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an i nvocation. ---> System.TypeInitializationException: The type initializer for 'CarveOutReason' threw an exception. ---> System.ArgumentException: Argument cannot be null or empty. (Parameter 'name')

using Ardalis.SmartEnum;

foreach (var item in new string[] { "V", "R", "S", "", "Z" })
{
    CarveOutReason.TryFromName(item, out var carveOutReason);
    Console.WriteLine(carveOutReason);
}

public sealed class CarveOutReason : SmartEnum<CarveOutReason>
{
    public string Code { get; set; }

    public static readonly CarveOutReason None = new("", 0);
    public static readonly CarveOutReason PharmacyVoid = new("V", 1);
    public static readonly CarveOutReason EligibilityChange = new("R", 2);
    public static readonly CarveOutReason SlowMover = new("S", 3);

    private CarveOutReason(string code, int value) : base(code, value)
    {
        Code = code;
    }

    public static bool Contains(string carveOutReason)
    {
        return List.Select(e => e.Code).Contains(carveOutReason);
    }
}
@arteny
Copy link

arteny commented Sep 21, 2022

Same issue. Exception is trown if List where value is empty string or null

@ardalis
Copy link
Owner

ardalis commented Sep 21, 2022

Hmm, I hadn't considered anyone would want to have an empty SmartEnum, since you can't AFAIK have an empty enum type. I don't suppose there's any reason why this couldn't be supported, though.

@arteny
Copy link

arteny commented Sep 21, 2022

Hmm, I hadn't considered anyone would want to have an empty SmartEnum, since you can't AFAIK have an empty enum type. I don't suppose there's any reason why this couldn't be supported, though.

In my case I am using SmartEnum for fixed string Statuses of some entity. And this statuses has predefined (3rd party) values, one of them is empty (which is something like Success means).

@MisinformedDNA
Copy link
Contributor

I could use this as well.

@MisinformedDNA
Copy link
Contributor

I'll create a PR.

@fiseni
Copy link

fiseni commented Apr 5, 2024

In the scope of enabling NRT, I'm going through old issues to understand the various requirements of users.
Here, I noticed that @Tyler-V is asking for empty/null Name support, not Value. In the related PR, we added support for nullable values instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants