What is the equivalence to getting an enum member by ordinal position when using EnumFormat?
See original GitHub issueHi Tyler,
Been watching this for a while and I hope it finally gets included as it is a great addition to .NET IMHO.
I am in the process of converting my (simple) enum helper and have the following that I don’t know how to convert:
/// <summary>
/// Class to assist with manipulating enums. These operate on the enum type as a whole and
/// are hence not extension methods for the enum members themselves.
/// </summary>
/// <typeparam name="T">Enumeration we are operating on.</typeparam>
public static class SpYtEnumHelper<T> where T : struct, IConvertible
{
/// <summary>
/// Internal helper method to ensure that the type we are dealing with is an enumeration.
/// </summary>
/// <returns>
/// Returns the type information for the enumeration. Raises an exception if the type supplied is not an
/// enumeration.
/// </returns>
private static Type CheckIsEnum()
{
Type result = typeof(T);
if (!result.IsEnum) throw new Exception("T must be an Enumeration type.");
return result;
}
/// <summary>
/// Given the ordinal position of the member within the enumeration we return the enum member.
/// </summary>
/// <param name="aOrdinalPosition">Zero based position of the member within the enumeration.</param>
/// <returns>Returns the enumeration member if found.</returns>
/// ///
/// <remarks>With an enum of MyEnum {Red = 3, Green = 5, Orange = 12} => GetMemberFromOrdinalPosition(1) = Green. </remarks>
public static T GetMemberFromOrdinalPosition(int aOrdinalPosition)
{
Type enumType = CheckIsEnum();
return (T) Enum.Parse(enumType, Enum.GetNames(typeof(T))[aOrdinalPosition]);
}
}
So given:
enum NumericOperator
{
[Symbol("="), Description("Is")]
Equals = 2,
[Symbol("!="), Description("Is not")]
NotEquals,
[Symbol("<")]
LessThan,
[Symbol(">="), PrimaryEnumMember] // PrimaryEnumMember indicates enum member as primary duplicate for extension methods
GreaterThanOrEquals,
NotLessThan = GreaterThanOrEquals,
[Symbol(">")]
GreaterThan,
[Symbol("<="), PrimaryEnumMember]
LessThanOrEquals,
NotGreaterThan = LessThanOrEquals
}
/// <summary>
/// Tests for SpYtEnumHelper.
/// </summary>
[TestFixture]
public class SpYtEnumHelperTests : SpYtTestsBase
{
[Test]
public void DummyTest()
{
//GetMemberFromValue
SpYtEnumHelper<NumericOperator>.GetMemberFromValue(3).ShouldBe(NumericOperator.NotEquals);
Enums.GetMember<NumericOperator>("3", EnumFormat.DecimalValue).Value.ShouldBe(SpYtEnumHelper<NumericOperator>.GetMemberFromValue(3));
//GetMemberFromOrdinalPosition
SpYtEnumHelper<NumericOperator>.GetMemberFromOrdinalPosition(0).ShouldBe(NumericOperator.Equals);
//Enums.GetMember<NumericOperator>("0", EnumFormat.EnumMemberValue).Value.ShouldBe(SpYtEnumHelper<NumericOperator>.GetMemberFromOrdinalPosition(0));
//Enums.GetMember<NumericOperator>("0", EnumFormat.UnderlyingValue).Value.ShouldBe(SpYtEnumHelper<NumericOperator>.GetMemberFromOrdinalPosition(0));
}
}
I am looking for the equivalence of my GetMemberFromOrdinalPosition() but none of the EnumFormat members appear to do that. While I realise I could do:
Enums.GetMembers<NumericOperator>().Skip(0).First().Value
I was hoping there was something more elegant I had missed?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Convert from enum ordinal to enum type - java
To convert an ordinal into its enum representation you might want to do this: ReportTypeEnum value = ReportTypeEnum.values()[ordinal];.
Read more >enum — Support for enumerations
is a set of symbolic names (members) bound to unique values · can be iterated over to return its canonical (i.e. non-alias) members...
Read more >Enumeration types - C# reference
An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type....
Read more >enum in Java
A Java enumeration is a class type. Although we don't need to instantiate an enum using new, it has the same capabilities as...
Read more >Converting Enum values in AL
There is a different way to get the ordinal value. Instead of using the Format function you can use the method AsInteger(). With...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@natiki Just as an FYI the recently released v3.0.0 now returns an
IReadOnlyList<T>instead ofIEnumerable<T>forGetNames,GetValues,GetMembers,GetFlags, andGetFlagMembers.Which is why I suggested it, because then ultimately the changes in terms of internal structure will be hidden from the caller and can be managed by Enums.NET.
However I will use your suggestions and you close this one. Looking forward to https://github.com/TylerBrinkley/Enums.NET/issues/26#issuecomment-487576627 and hope Enums.NET gets up before .NET 5 😉