Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:04:33+00:00 2026-05-27T19:04:33+00:00

In my program, I am using enumerations to represent the current state of an

  • 0

In my program, I am using enumerations to represent the current state of an object. I would like to be able to cycle through the values of this enumeration one by one. The fact that the enum values are numerically sequential and begin with 0 is a given.

While attempting to write a function which increments the enumeration, looping if it has reached the end has yielded a problem.

public static TEnum IncrWrap<TEnum>(TEnum value)
{
    int len = Enum.GetNames(typeof(TEnum)).Length;
    dynamic wrapper = value;
    dynamic newValue = ((int)wrapper + 1) % len;
    return (TEnum)newValue;
}

If the IncrWrap method exists in the same assembly (project) as the object which calls it, it works perfectly. However, if I move it to another project (higher up so that more things can make use of it) and call it from the dll, I get a RuntimeBinderException when it attempts to cast wrapper to an int with the message “Cannot convert type ‘System.Enum’ to ‘int'”.

Whats up? I don’t see why calling the function from a different assembly would cause the function to behave so differently. Is there something about dynamic I don’t understand? It would be nice to not have to use reflection to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:04:34+00:00Added an answer on May 27, 2026 at 7:04 pm

    Unfortunately enumerations pose some problems here. First of all an enumeration can have any integral type, other than char. Secondly, there is no guarantee that enumerations have incrementally increasing values of the underlying type. Lastly, the C# language does not support generic type constraints of System.Enum which would be somewhat helpful. With all that said, I believe that you can solve your problem a bit differently:

    public static TEnum NextEnum<TEnum>(TEnum value)
    {
        if (!typeof(TEnum).IsEnum)
            throw new ArgumentException("Expected type of System.Enum", "value");
    
        var values = Enum.GetValues(typeof (TEnum));
        var index = Array.IndexOf(values, value);
        return (TEnum)Enum.ToObject(typeof (TEnum), values.GetValue((index + 1) % values.Length));
    }
    

    I’m actually surprised that you were able to get that to work using your dynamic solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Scala 2.8 and defining an Enumeration like this: object Stooges extends Enumeration
I have a Java program using AWT which I would like to run on
I have a c++ program using winsock2. I would like to know how to
I was trying to compile a program using an external compiled object coreset.o. I
I'm developing automation program using winforms webbrowser control. I am able to get the
I would like to use enumerations in my dynamic LINQ queries. Is it possible,
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
I wrote a program using AutoIT to fetch information from a number of websites
Suppose I write a program using immutable data structures in Java. Even though it
When programming an OO program using a SQL database back-end, do objects' attributes correspond

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.