question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the equivalence to getting an enum member by ordinal position when using EnumFormat?

See original GitHub issue

Hi 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TylerBrinkleycommented, Nov 7, 2019

@natiki Just as an FYI the recently released v3.0.0 now returns an IReadOnlyList<T> instead of IEnumerable<T> for GetNames, GetValues, GetMembers, GetFlags, and GetFlagMembers.

0reactions
natikicommented, May 14, 2019

I don’t think it’s a good fit for EnumFormat, especially as the notion of ordinal might change slightly in the future to match System.Enum which returns values in increasing bit-significance order as opposed

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 😉

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found