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 8624089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:26:20+00:00 2026-06-12T07:26:20+00:00

I’m writing some code with a simple switch statement based on Enum values. It

  • 0

I’m writing some code with a simple switch statement based on Enum values. It occurred to me that at some point in future a developer may add a new value, so I included a default method to capture this at runtime and throw an exception. However I realised that I should do this every time I put in logic like this, and that I’d only see such issues at run time rather than compile time.
I’m wonderring if there’s some code I can add to get the compiler to tell the developer that they need to update certain methods in the case that they update enum values – beyond just adding comments to the enum itself?

e.g. (the example below’s purely theoretical; I chose statuses from the development lifecycle to ensure it’s something familiar to most).

public enum DevelopmentStatusEnum
{
    Development
    //, QA //this may be added at some point in the future (or any other status could be)
    , SIT
    , UAT
    , Production
}

    public class Example
    {
        public void ExampleMethod(DevelopmentStatusEnum status)
        {
            switch (status)
            {
                case DevelopmentStatusEnum.Development: DoSomething(); break;
                case DevelopmentStatusEnum.SIT: DoSomething(); break;
                case DevelopmentStatusEnum.UAT: DoSomething(); break;
                case DevelopmentStatusEnum.Production: DoSomething(); break;
                default: throw new StupidProgrammerException(); //I'd like the compiler to ensure that this line never runs, even if a programmer edits the values available to the enum, alerting the program to add a new case statement for the new enum value
            }
        }
        public void DoSomething() { }
    }
    public class StupidProgrammerException: InvalidOperationException { }

This is a bit academic, but I can see it as being useful in making my app robust. Has anyone tried this before / got any good ideas on how this might be achieved?

Thanks in advance,

JB

  • 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-12T07:26:21+00:00Added an answer on June 12, 2026 at 7:26 am

    In such a case I would try not to use an enum but rather a class with public static readonly fields which are instances of the class. See what the .Net framework does with colors for example. There is a class Color and you can use objects like Color.Black, Color.Blue, etc. They’re not constants but offer almost all the same benefits. Plus they have other benefits that constants don’t have. See the C# version 3 language specification which also talks about this a bit.

    But the idea is that you don’t have a case statement. You add enough other properties to each “enum” member that the method (DoSomething or whatever) can treat it properly. When another developer wants to add another member object, he needs to supply the attributes needed. My example: I needed an “enum” for the different actions a user can take in the system. These actions needed to be checked for permissions, logged, etc. I also needed parent and child actions (renaming something is “part of” editing it, etc.), abstract actions used for grouping actions together for filtering purposes, and special actions “All” and “None” (None being undefined). Each one needed an ID and text in the database as well. I wanted it to still work if someody invented a new type of action. What I did is something like this (lots of code omitted just to give you the idea):

      public class Action
      {
        protected Action(bool Abstract, Action Parent, int ID, string Name, bool Undefined)
        { /* snip */ }
        protected Action(bool Abstract, Action Parent, int ID, string Name)
          : this(Abstract, Parent, ID, Name, false)
        { }
        //----------------------------------------------------------------------------------------
        public static readonly Action All = new Action(true, null, 0, "All");
        public static readonly Action None = new Action(false, All, 6, "(Undefined)", true);
        public static readonly Action Modifying = new Action(true, All, 1, "Modifying");
        public static readonly Action Creating = new Action(false, Modifying, 2, "Creating");
        public static readonly Action Deleting = new Action(false, Modifying, 3, "Deleting");
        public static readonly Action Editing = new Action(false, Modifying, 4, "Editing");
        public static readonly Action Exporting = new Action(false, All, 5, "Exporting");
        public static readonly Action Renaming = new Action(false, Editing, 7, "Renaming");
        /* snip */
        //----------------------------------------------------------------------------------------
        /* template for new entries:
        public static readonly Action  = new Action(false, All, , "");
        */
      }
    

    There are more actions. And there are a number of methods in other classes that operate on actions. They all keep working as long as each action provides the information needed. The developer adding an action is forced to provide the information. If the current attributes are not enough for some future “special” action then more attributes will have to be added later. Note that the constructors are protected, so only the class itself can create actions. I omitted a lot of code in the main constructor that checks for duplicate IDs and Names and lots of other things. You can now use it like this:

    Log.LogAction(Action.Renaming);
    

    The method LogAction doesn’t have a case statement in it. It uses the attributes of the action.

    What is your enumeration?

    Regards

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a small JavaScript validation script that validates inputs based on Regex. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.