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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:58:29+00:00 2026-05-17T18:58:29+00:00

I have an example like this class A { A() {} public C createC

  • 0

I have an example like this

class A {

   A() {}
   public C createC () {
      ...
   }

} 

class B {

   B() {}
   public C createC () {

   }
}

Objects for A and B are created based an an

public enum D { ii, jj };

And I see code all over the place like

D d; 

switch (d) {
    case ii: (new A()).createC(); break;
    case jj: (new B()).createC(); break;
 };

How can I avoid the switch cases all over the place? I understand the code is not so clear.

  • 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-17T18:58:30+00:00Added an answer on May 17, 2026 at 6:58 pm

    I would be inclined to add the create code to the enum.

    enum D {
        ii {
            public void createC() { new A().createC(); }
        },
        jj {
            public void createC() { new B().createC(); }
        };
    
        public abstract void createC();
    }
    

    You can then replace the switch with

    d.createC();
    

    Having said that I would also look at making the createC method static or moving the createC code to the enum rather than leaving it in the A and B classes.

    If you make the createC method in A and B static the enum would look like

    enum D {
        ii {
            public void createC() { A.createC(); }
        },
        jj {
            public void createC() { B.createC(); }
        };
    
        public abstract void createC();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example I have a simple class like public class Person{ public int Age
I have a custom c# type like (just an example): public class MyVector {
I have a Struts + Velocity structure like for example, a Person class, whose
I have a SQL table like so: Update: I'm changing the example table as
Say I have a site on http://example.com . I would really like allowing bots
How do I create subdomain like http://user.mywebsite.example ? Do I have to access .htaccess
I have a few tables that I've defined like the below examples: class TableA
Suppose I have an XML-serializable class called Song : [Serializable] class Song { public
Can anybody tell me why is this not working. I have created a WCF
I have found this example on StackOverflow: var people = new List<Person> { new

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.