Home C# select Class during Runtime
Reply: 0

C# select Class during Runtime

user1327 Published in May 22, 2018, 4:36 am

I have a C# Project for ProjectEuler if you know that.

For that i use a abstract class lets call it 'X'

public abstract class X {
protected abstract void SomeFunction()

There are over 600 problems to solve in this project. For every problem, I create a class that inherits from X. The name of the class is always the 'Baseclassname' followed by a number


public class X2 : X {
public override void SomeFunction()

During runtime, I choose via consoleinput a problem by entering a valid integer. This integer goes into a switch and creates the corresponding problem and executes someFunction if '_problem' is unequal to null.

X _problem = null;
int someInput; //any valid input readin via Console
case 1:
_problem = new X1();
case 2:
_problem = new X2();
case 610:
_problem = new X610();

So my question to the community is how to avoid a switch case with over 600 cases. Is there a better solution to do this?

I hope this question is written well cos it's my first. :)

Edit: Some 'key, value' Pair array would only be ideal if all existing pairs get added automatically during compilation.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314209 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO