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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:28:54+00:00 2026-05-31T14:28:54+00:00

I have about 20 classes which are derived from ConvertApi classes. Every class share

  • 0

I have about 20 classes which are derived from ConvertApi classes. Every class share Convert method from parent class and has unique property SupportedFiles. I use these classes for file manipulation and my code looks like

if (fileEx=="txt")
   new Text2Pdf().Convert(file)
else
  if (fileEx=="doc")
    new Word2Pdf().Convert(file)
     else
    //and so on..

I know that these code can be optimized because 20 times if operator is repeated and that looks bad, but can’t find a way to do that. Could anyone help me?

class Text2Pdf : ConvertApi
{
  enum SupportedFiles { txt, log }; 
}

class Word2Pdf : ConvertApi
{
  enum SupportedFiles { doc, docx }; 
}

class Excel2Pdf : ConvertApi
{
  enum SupportedFiles { xls, xlsx }; 
}

class ConvertApi
{
 public void Convert(....);
}
  • 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-31T14:28:55+00:00Added an answer on May 31, 2026 at 2:28 pm

    In your base class, have something like this:

    public abstract class ConvertApi
    {
        protected abstract string[] SupportedFilesImpl();
    
        public bool Supports(string ext)
        {
            return SupportedFilesImpl.Contains(ext);
        }
    }
    

    Now, your derived classes can implement this method:

    public class Text2PDF : ConvertApi
    {
        protected override string[] SupportedFilesImpl { return new string[] { "txt", "log"}; }
    }
    
    public class Doc2PDF : ConvertApi
    {
        protected override string[] SupportedFilesImpl { return new string[] { "doc", "docx"}; }
    }
    

    … and so on for the rest of the converters. Then put these in a list…

    List<ConvertApi> converters = new List<ConvertApi>();
    converters.Add(new Text2PDF());
    converters.Add(new Doc2PDF());
    

    (Note, I’d probably have a class containing these rather than just a list, but anyway). Now, to find a converter:

    foreach(ConvertApi api in converters)
    {
        if(api.Supports(fileExt))
        {
            // woo!
            break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data object which has a base class with three derived classes,
I have an abstract base class from which many classes are derived. I want
I have a superclass which instantiates my Sql class so all derived classes will
I'm developing an application which will have these classes: class Shortcut { public string
I have an abstract class CommandPath, and a number of derived classes as below:
I have two classes: a base class, Foo::Base and a derived class, Foo::Base::Sub .
This question is about overriding a virtual method in a derived class with a
I have a class PricingModel with two derived classes: Recurring and Packaged. in my
I have an abstract virtual base class Foo from which I derive many other
What I'm talking about here are nested classes. Essentially, I have two classes that

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.