ardalis / ardalis/SmartEnum

An item with the same key has already been added. When using two sub-classes from abstract SmartEnum class.

Open
#274 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
2.4k
Forks
184
PR merge metrics
No merged PRs in 30d

Description

Hello @ardalis, Your library are very awesome and incredible!
I got a little problem here when try to accessing enum List property. Actually property works as static and it filled with all enums value of all classes hierarchy not the one that am target!

I think Items dictionary need to be enhancing. Because In my case I got exception
"An item with the same key has already been added."

I have following scenario:

1. Enum Class called : License
2. two sub-classes one called `AppOneLicense:License`
3. second one called `AppTwoLicense:License`

An enum `abstract License class` used as base of `sealed AppOne, AppTwo classes`

Now when AppOne have enum with value of (1 or StandardLicense)
And AppTwo have enum with value (1 or TrialLicense)

Exception thrown. Because flags are the same...

Here's implementation you can try it:

public abstract class LicenseEdition : SmartEnum
{
protected LicenseEdition(string name, byte value) : base(name, value)
{
}
}

```
public class MyLicenseEditionAppOne : LicenseEdition
{
public static readonly MyLicenseEditionAppOne Trial = new MyLicenseEditionAppOne(nameof(Trial ), 1); // NOTE!!
public static readonly MyLicenseEditionAppOne Standard = new MyLicenseEditionAppOne(nameof(Standard), 2);
public static readonly MyLicenseEditionAppOne Professional = new MyLicenseEditionAppOne(nameof(Professional), 3);
public static readonly MyLicenseEditionAppOne Ultimate = new MyLicenseEditionAppOne(nameof(Ultimate), 4);
public MyLicenseEditionAppOne(string name, byte value) : base(name, value)
{
}
}
```

public class MyLicenseEditionAppTwo : LicenseEdition
{
public static readonly MyLicenseEditionAppTwo Standard = new MyLicenseEditionAppTwo(nameof(Standard ), 1); // NOTE!!
public MyLicenseEditionAppTwo(string name, byte value) : base(name, value)
{
}
}

That's a problem occurred :sob:
The property `List` need to determine a deal with calling class hierarchy level itself. Not just static base one `SmartEnum<>`,
When I need to access `MyLicenseEditionAppOne.List` it need to return a list of `AppOne` enums only not whole SmartEnum<> classes I think...

Interanlly I think `List` calling _fromName dictionary. which take name as a key!
So, If MyLicenseEditionAppOne and MyLicenseEditionAppTwo have same string key (Example `Standard` name). the Exception thrown then. Maybe you can enhance the _fromName to take a tuple of <(Type, String), TEnum>.

I need to overcome that issue (maybe from my side classes above need to re-design.. to accept same name in dervied enums)

![image](https://user-images.githubusercontent.com/33269552/170980641-64d57345-7a7e-4389-9ded-5a5403ac8834.png)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the two derived LicenseEdition classes shown in the report and inspect SmartEnum's List property and internal _fromName dictionary. Determine whether derived types should maintain separate collections when names or values overlap, then verify that MyLicenseEditionAppOne.List excludes MyLicenseEditionAppTwo entries without breaking existing SmartEnum behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.