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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:13:40+00:00 2026-05-25T18:13:40+00:00

I may miss some sort of point here, if that’s the case – please

  • 0

I may miss some sort of point here, if that’s the case – please include that discussion as a part of my question :).

This is a shortened down and renamed sample of a working code. The GetTicks(…) is a single sample, which could be any sort of functionality (value of > 0 < 9 should return a specific Enum a.so).

public static class Something
{
    public enum TypeOf : short
    {
        Minute = 2, Hour = 3, Day = 4, …
    }

    public static long GetTicks(Something.TypeOf someEnum)
    {
        long ticks = 0;
        switch (someEnum)
        {
            case Something.TypeOf.Minute:
                ticks = TimeSpan.TicksPerMinute;
                break;
            case Something.TypeOf.Hour:
                ticks = TimeSpan.TicksPerHour;
                break;
         ....
        }
        return ticks;
    }
}

// This class is called from anywhere in the system.
public static void SomeMethod(string dodo, object o, Something.TypeOf period)
{
    // With the design above
    long ticks = Something.GetTicks(period);

    // Traditional, if there was a simple enum
    if (period == Something.Day)
        ticks = TimeSpan.FromDays(1).Ticks;
    else if (period == Something.Hour)
        ticks = TimeSpan.FromHours(1).Ticks;
}

The idea is to collect functionality that concerns an enum, near as possible to the enum itself. The enum is the reason function. Also, I find it easy and natural to look for such functionality near the enum. Also, it’s easy to modify or extend.

The drawback I have is that I have to state the enum more explicit, like Something.TypeOf. The design may look non-standard? And would it apply, if the enum was for internal use in the class.

How would you do this more nicely?
I tried abstract, base inheritance, partial. None of them seem to apply.

  • 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-25T18:13:40+00:00Added an answer on May 25, 2026 at 6:13 pm

    C# enums don’t work well like this. However, you can implement your own “fixed set of values” fairly easily:

    public sealed class Foo
    {
        public static readonly Foo FirstValue = new Foo(...);
        public static readonly Foo SecondValue = new Foo(...);
    
        private Foo(...)
        {
        }
    
        // Add methods here
    }
    

    As it happens, one example I’ve got of this is remarkably similar to yours – DateTimeFieldType in Noda Time. Sometimes you might even want to make the class unsealed, but keep the private constructor – which allows you to create subclasses only as nested classes. Very handy for restricting inheritance.

    The downside is that you can’t use switch 🙁

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

Sidebar

Related Questions

May be some body already used some open source component, writing on jquery. And
May be it sound dumb but if I want some computed value from other
Edit: C# 3.0, net 3.5. I am C++ programmer, so maybe I miss some
I love text mate, but sometimes I do miss some IDE features. One of
Is it possible that some calls publishProgress missed by onProgressUpdate? I mean is it
I have a batch script I run before our performance tests that does some
A pretty simple question really. Are queries consistent across instances? I understand that with
Good afternoon, Many of you may already know that it is possible, for instance,
I may miss the obvious, but how/is it possible to retrieve interrupt counters for
First off, I know this may be a very stupid question, so don't shoot

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.