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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:12:04+00:00 2026-05-16T06:12:04+00:00

I have the following class: public class DocketType : Enumeration<DocketType, int, string> { public

  • 0

I have the following class:

public class DocketType : Enumeration<DocketType, int, string>
{
    public static DocketType ChangeOver = new DocketType(1, "Changeover");
    public static DocketType Withdrawal = new DocketType(2, "Withdrawal");
    public static DocketType Installation = new DocketType(3, "Installation");

    private DocketType(int docketTypeId, string description) 
        : base(docketTypeId, description)
    {
    }
}

With the following base class:

public abstract class Enumeration<TEnum, X, Y> : IComparable 
    where TEnum : Enumeration<TEnum, X, Y> 
    where X : IComparable 
    where Y : IComparable
{        
    protected Enumeration(X value, Y displayName)
    {
        AddToStaticCache(this);
    }
    public static TEnum Resolve(X value)
    {
        return Cache[value] as TEnum;
    }
}

The problem I have is that Changeover, Withdrawal and Installation are not being created when the first time that the static class is used is via the Resolve method in the base class. I.e. if I call Resolve, then Cache will be empty.

However, if I do something like DocketType foo = DocketType.Changeover; in Application_Start, then all of the static fields get created and then Cache has all three values.

What’s the correct way to create these static fields so this scenario works?

  • 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-16T06:12:05+00:00Added an answer on May 16, 2026 at 6:12 am

    I don’t think the fields in DocketType should be initialised when all you’re accessing is Enumeration<>. You are not referencing the DocketType type at all when you call Enumeration<>.Resolve(). Should the CLR really initialise all subclasses every time you access a static method or static field? It would slow down your code, and in most cases unnecessarily so.

    You could try writing Docket.Resolve(), which C# allows you to do, but I don’t know whether this will compile into something different than before; the compiler might just turn it into Enumeration<DocketType, int, string>.Resolve() and you’re back to sqaure one.

    To be honest, I am inclined to suggest that your code structure is flawed, and the problem you’re running into is a symptom of that. You shouldn’t have to rely on Cache containing something. You shouldn’t have to rely on some static type initialisation to have occurred when you’re not using that type.

    Therefore, your options are:

    • Put a pointless reference to DocketType somewhere in your Main() method to ensure the initialisation happens, and live with the idea that your code structure may be flawed.
    • Move the static fields to another type, perhaps Enumeration<> itself, which alleviates the flaw but doesn’t completely solve it.
    • Think about the fundamental structure of your code and redesign it so that you don’t have to rely on the cache being filled.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have the following class public class MyClass { private int myAttr; public void
I have the following class: public class Test { public static int a =
Lets say i have following class public class abc { int id; string name;
I have a following class public class MyHttpClient { private static HttpClient httpClient =
I have following class: public class PairOfDice { private Dice d1,d2; public int Value
I have a following class public class People { public int id; public string
I have the following class: public abstract class AbstractParent { static String method() {
I have the following class public class Element { public List<int> Ints { get;private
I have following class ( entity ) @Entity public class Magazine { private int

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.