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

  • SEARCH
  • Home
  • 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 8168443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:31:38+00:00 2026-06-06T20:31:38+00:00

In the stub below how do I pass in (MyEnum) as a parameter such

  • 0

In the stub below how do I pass in (MyEnum) as a parameter such that I can use this procedure with any enum?

public static Enum Proc(this Enum e)
{
Int32 i = (Int32)(MyEnum)e;
...

Here is the solution I have come up with that works:

public static Enum Next(this Enum e, Type eT)
{
  Int32 i = (Int32)(Object)e;   
  return (Enum)Enum.Parse(eT, Enum.GetName(eT, Enum.GetName(eT, ++i) == null? i = 0 : i));  
}

This solution isn’t ideal because I have to do this to get the next value:

MyEnum e = (MyEnum)e.Next(typeof(MyEnum));

I’d rather just do

MyEnum e = e.Next(typeof(MyEnum));

Or even better:

MyEnum e = e.Next();

Anyone who can provide the simple solution can have the point.

Also the code I’ve written above runs fine in LinqPad but only compiles in WP7 and then throws an exception when I run it (InvalidProgramException).

  • 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-06-06T20:31:40+00:00Added an answer on June 6, 2026 at 8:31 pm

    Here’s a function that will cycle through the values of any enum:

    static public Enum Cycle(this Enum e)
    {
        bool found = false;
        Enum first = null;
        foreach (Enum i in Enum.GetValues(e.GetType()))
        {
            if (first == null)
                first = i;
            if (found)
                return i;
            found = e.Equals(i);
        }
        if (found)
            return first;
        return null;
    }
    

    For versions of C# that don’t have Enum.GetValues, you’ll have to use a method like this which only works for enums whose values start at 0 and increment by 1:

    static public Enum Cycle(this Enum e)
    {
        var i = ((IConvertible)e).ToInt64(null) + 1;
        var eT = e.GetType();
        var next = Enum.GetName(eT, i);
        return (Enum)Enum.Parse(eT, next ?? Enum.GetName(eT, 0), false);
    }
    

    Use it like:

    var nextEnum = (MyEnum)curEnum.Cycle();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I'm mocking a class, like below, is there any way I can get
I want to use stub entities but it seems I cannot use TryGetObjectStateEntry without
Below is the plugin code that i have tried to reuse from an old
Below is the problem I am having. I am not sure how to pass
The test class below verifies that a simple HttpService gets content from a given
I am trying to extends the AbstractImagePrototype as below: public class DynamicImagePrototype extends AbstractImagePrototype
How do I stub an http request, like this one to the twitter api
I get the below error in the line this.viewcam.setOnClickListerner(new view.OnClickListerner()) . I get this
How can I convert the below int to display the result of the sum
I wrote a Multithreaded program in Java given below :- public class Client {

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.