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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:54:19+00:00 2026-05-22T02:54:19+00:00

Greetings everyone! I’ll try to make my problem simple: I have an enum to

  • 0

Greetings everyone!

I’ll try to make my problem simple: I have an enum to select which ObjType I should use (ObjTypeA and ObjTypeB both inherits from ObjType). So I created a method to extend the given enum, in order to return a new instance according to the selected property in the enum, like follows in the code. I think it works more or less like a factory design pattern. So far so good, but eventually, like in the class MyClass, I may attempt to create n instances of ObjTypeA or ObjTypeB, but I’ll have to face the if statement everytime I call the GetObjTypeInstance() method. So:

  • Can an enum return an instance, something like: public enum EObjType { ObjTypeA = new ObjTypeA(), ObjTypeB = new ObjTypeB() }? Actually, it’d be better to append some GetInstance() method to the ObjTypeA and to the ObjTypeB options in the enum. If there’s a way to do this, how can I do it? Doing this I’d avoid those if statements every while step.
  • Is there any other (and better) way to this this (if you understood my problem…)? How?

Thanks in advance!

Follow the example code:

public static class EObjTypeExt
{
    public static ObjType GetObjTypeInstance(this EObjType ot)
    {
        if (ot == EObjType.ObjTypeA)
        {
            return new ObjTypeA();
        }
        else if (ot == EObjType.ObjTypeB)
        {
            return new ObjTypeB();
        }
        throw new ArgumentOutOfRangeException("unrecognized type!");
    }
}

public enum EObjType { ObjTypeA, ObjTypeB }

public class MyClass
{
    ObjType[] obj { get; set; }

    public MyClass(EObjType otEnum, int n)
    {
        this.obj = new ObjType[n];
        int i = 0;
        while (i < n)
        {
            this.obj[i] = otEnum.GetObjTypeInstance();
            i++;
        }
    }
}
  • 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-22T02:54:20+00:00Added an answer on May 22, 2026 at 2:54 am

    Instead of using an enum, I would use a class that looks like an enum:

    public class EObjType {
        public static readonly EObjType ObjTypeA = new EObjType(() => (ObjType)(new ObjTypeA));
        public static readonly EObjType ObjTypeB = new EObjType(() => (ObjType)(new ObjTypeB));
    
        private readonly Func<ObjType> generator;
        private EObjType(Func<ObjType> generator) {
            this.generator = generator;
        }
    
        public ObjType GetInstanceOfObjType() {
            return generator();
        }
    }
    

    You can then use it exactly as you have been the enum.

    EObjType otEnum = EObjType.ObjTypeA;
    ObjType obj = otEnum.GetInstanceOfObjType();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings everyone, I'm having a very strange problem. I've created a DateTextBox which automaticly
Greetings everyone I have this regular expression which goes as follow: $thread_views_exp = '~<td
Greetings everyone! Question : does any one have a success with combining Microsoft Visual
Greetings everyone... If I have the following interface: interface IMyInterface { int property {
Greetings everyone! I have a set of a few (and potentially will have dozens
Greetings. I have an 2-D array of size [N][N] which will represent a rectangular
Greetings I have an ajax call which: Gets an xml file of posts Populates
Greetings, everyone. I'm trying to learn some Silverlight basics, and have decided to write
Greetings everyone! I programmed GUI Application using Java Swing under Windows. Under windows everything
Greetings Everyone. I'm currently writing a multi-language programe in C, C++ and fortran on

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.