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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:20:09+00:00 2026-05-16T03:20:09+00:00

I’ve created my own custom pseudo enumerations within my domain model to allow me

  • 0

I’ve created my own custom pseudo enumerations within my domain model to allow me to have some more verbose values. For example, my class is as follows:

public abstract class Enumeration<X, Y> : IComparable where X : IComparable
{

    public Enumeration(X value, Y displayName) { }

    public Y DisplayName { get { return _displayName; } }
    public X Value { get { return _value; } }

}

And a class that inherits it would be:

public class JobType : Enumeration<string, string>
{
    public static JobType ChangeOver = new JobType("XY01", "Changeover");
    public static JobType Withdrawal = new JobType("XY02", "Withdrawal");
    public static JobType Installation = new JobType("XY03", "Installation");

    private JobType(string jobTypeId, string description)
        : base(jobTypeId, description) { }
}

The problem I’ve got is that I want to be able to resolve these values from the value returned from the database in my repository. So I’m after a method such as:

public static JobType Resolve(string jobTypeId) { return matchingJobType; }

I started writing a resolve method for each enumeration class, but there’s got to be a better way than duplicating the same method with a switch statement in each?

I thought about adding a Dictionary<X, Enumeration<X, Y>> Cache; property to the base class, and adding the class into it from the constructor of the base class. This would also have the benefit of ensuring unique values. The problem I have with this is that when I get the enumeration from the Dictionary, it’s of the type Enumeration<X, Y> and I want it as a JobType.

So this means having to either add a third generic type to the Enumeration class and having:

public static T Resolve(X value); // With the additional type
public static T Resolve<T>(X value); // Or without an additional type

I obviously don’t like the idea of having to write JobType.Resolve<JobType>(foo);, and what I want is just JobType.Resolve(foo);, but it should be done with as little code as possible. I.e. can all this just be handled from the base class without having to include an additional generic type?

  • 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-16T03:20:09+00:00Added an answer on May 16, 2026 at 3:20 am

    This looks like a situation for the curiously recurring template pattern. If you want to get the base type method to return the derived class without casting, then you can pass the derived type into the base type, constraining the derived type to to be derived from the base type, e.g.

    public abstract class Enumeration<TEnum, X, Y> : IComparable 
        where TEnum : Enumeration<TEnum, X, Y>
        where X : IComparable
    {
        public static TEnum Resolve(X value) { /* your lookup here */ }
    
        // other members same as before; elided for clarity
    }
    

    You’d then define your concrete classes as follows.

    public class JobType : Enumeration<JobType, string, string>
    {
        // other members same as before; elided for clarity
    }
    

    Now your types will match up and no casting required on the base class Resolve method.

    JobType type = JobType.Resolve("XY01");
    

    How you store the value to instance mapping in the base class is up to you. It sounds like you already know how to do this anyway, and just needed a bit of help with getting the types to match up.

    • 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 have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have a text area in my form which accepts all possible characters from
Basically, what I'm trying to create is a page of div tags, each has

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.