Comments (9)
The name is the key. The value is, well, the value.
It does follow the convention of dealing with the key-value pairs
from smartenum.
I wouldn't add an implementation in the SmartEnum class that was tied to SelectList or any other UI element. I could see adding either samples showing how to use SmartEnum with a SelectList or even including some extension methods that would create a SelectList from a SmartEnum or something like that. Would that help?
from smartenum.
Isn't it inefficient to use a string as key?
And I personally find it counter-intuitive to think about the "name" as the variable supposed to hold my "key" and the "value" holding the display name or other info...
When rendering a select list for example, the current nomenclature forces me to create a SelectList using the "name" as value and "value" as the text field... that doesn't sound logical to me.
Also, on the way back, I should then use a string as the type to bind the result to? To me it doesn't seem logical at all, but that's my own opinion. Also related to this point, would it be possible to bind to the enum type when using asp.net core model binding? (okay maybe that's out of the scope of this question)
Thanks for your input though.. I'l try to wrap my head around the inconsistency (IMO) :)
from smartenum.
For a SelectList you want to display the Name but use the Value as the SelectList’s Value, right? I’m not seeing the inconsistency. Can you demonstrate with a bit of code perhaps?
from smartenum.
You are totally right and I never said otherwise (sorry if I was unclear about that).
The only thing that bothered me in the beginning (as mentioned in the first post) was the initialization in which the "value", which is equivalent to the key, was put after the name. I would have liked to align the keys/values at the beginning as shown below to be able to see quickly the keys used, but that's a really minor thing ahah.
public class TestEnumClass : SmartEnum<TestEnumClass, int>
{
public static readonly TestEnumClass FirstKey = new TestEnumClass(0, "Display name for first key");
public static readonly TestEnumClass SecondKey = new TestEnumClass(1, "Name for second item");
public static readonly TestEnumClass ThirdKey = new TestEnumClass(2, "etc...");
private TestEnumClass(string name, int value) : base(name, value){}
}
I guess I read it at first as if the first param was indeed treated as the enum's equivalent key, but it's not, it's the enum's equivalent "textual" value, which is totally fine, and the value is treated the same as the underlying type, which is effectively an int in most cases. To be totally honest now that I look back at it, I wonder why I thought it was inconsistent. I guess I was half awake and the confusion came from the fact the here the "name" is used as a key, corresponding to the standard Enum's textual value, while in my head, the key was the "underlying value", which was effectively related to the standard Enum's value... don't know if it's any clearer ¯\(ツ)/¯
Sorry for bothering :)
from smartenum.
Yes, the name is just a textual representation of the key. The key is actually the static field in the class (TestEnumClass.FirstKey
). The name is supposed to represent that name or be meaningful in some way. Usually it is some variant of the variable name. This is why the README uses nameof()
so that if the variable is renamed, the textual "key" remains the same as the variable name.
The name can be whatever you want it to be. If you're going to use it for a list and want to store the displayed message in the SmartEnum, I'd suggest extending SmartEnum and adding a description property so that you can use it in a list.
public TestEnumClass : SmartEnum<TestEnumClass, int>
{
public static readonly TestEnumClass FirstKey = new TestEnumClass(nameof(FirstKey), 0, "Display name for first key");
public static readonly TestEnumClass SecondKey = new TestEnumClass(nameof(SecondKey), 1, "Name for second item");
public static readonly TestEnumClass ThirdKey = new TestEnumClass(nameof(ThirdKey), 2, "etc...");
public string Description { get; }
private TestEnumClass(string name, int value, string description) : base (name, value) {
Descriptions = description;
}
}
from smartenum.
That's exactly what I did :)
from smartenum.
Would it be useful to add a default implementation in the SmartEnum class that would get the SelectList from the value and name of the generated list directly?
from smartenum.
from smartenum.
Related Issues (20)
- Methods FromName() and TryFromName() for SmartFlagEnum do not work with ignore case at true
- How do i use this with NSwag / fast endpoints ? HOT 3
- Deserialization of appsettings.json via ConfigurationBuilder doesn't work HOT 1
- EF Core 7 InMemory - Throwing System.ArgumentException with any query involving a SmartEnum HOT 3
- README Example does not compile
- Case Insensitive String Enum HOT 1
- Enable Discussions for repo
- Loading SmartEnum from Entity Framework without specifying it as a static property
- Not compatible with .Net 6 HOT 1
- SmartFlagEnum does not support values larger than int.MaxValue HOT 1
- Need simple method to get single int value from existing IEnumerable<MyFlagEnum>. HOT 5
- SmartEnum binding to [FromQuery] parameters HOT 3
- Ordering the values
- Can/should SmartEnum implement the non-generic IComparable interface?
- SmartEnumStringComparer not included in 7.0.0 version HOT 2
- Add a ValidationAttribute to Validate Model Property Against SmartEnum Names
- ushort error in SmartFlagEnumValueConverter HOT 1
- For the README.md I found out that the path for TestEnum.cs is broken HOT 1
- Add Description attribute HOT 1
- Please Release Version for .NET 8.0 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from smartenum.