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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:14:21+00:00 2026-05-16T12:14:21+00:00

I have a set of classes ( MyClass1, MyClass2, MyClass3 ) that implement an

  • 0

I have a set of classes (MyClass1, MyClass2, MyClass3) that implement an interface (IMyClass).

I am interested in creating a method to fill and return a list (Collection.Generics.Lists<>) of objects that have common interface MyClassX (List<IMyClass>). But how?

Each class has a constructor MyClassX which does something different. So I can not create a BaseClass and use a common constructor for all.

By interface I can not do:

public List<IMyClass> Fill(string[] ItemsPassedByParam, IMyClass InstanceOfMyClassX)
    List MyList = new List<IMyClass>();
    foreach (item in ItemsPassedByParam)
    {
        MyList.Add (new IMyClass (item)); //can't do new from an Interface!!
    }
}

Also I tried using generics type, TMyClass in the next example, but neither has worked for me (though I have not very deep notions about generic types):

public List<TMyClass> Fill(string[] ItemsPassedByParam, Type TypeOfMyClassX)
    List MyList = new List <TMyClass> ();
    foreach (item in ItemsPassedByParam)
    {
        MyList.Add (new TMyClass (item)); //can't do new from a Generic Type!!
    }
}

I tried also to externalize the code inside the Constructor of MyClassX in a method, but I can’t call the method since de object is not initialized (there is no new () ).

In these cases which is the solution?

Thanks in advance!

  • 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-16T12:14:21+00:00Added an answer on May 16, 2026 at 12:14 pm

    It sounds like you want the Fill method to create a List<T> from the passed in string values where T is a derivation of IMyClass.

    What you need to do is pass a factory function into the Fill method which allows for the creation of IMyClass derived instances.

    public List<IMyClass> Fill(
      string[] ItemsPassedByParam, 
      Func<string,IMyClass> createFunc)
    
      List MyList = new List<IMyClass>();
      foreach (item in ItemsPassedByParam) {
        MyList.Add(createFunc(item));        
      }
    }
    

    Then at the place you call Fill you can choose which derivation of IMyClass to use

    string[] items = ...;
    List<IMyClass> list1 = Fill(items, item => new MyClass1(item));
    List<IMyClass> list2 = Fill(items, item => new MyClass2(item));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If have a set of classes that all implement an interface. interface IMyinterface<T> {
I have a set of callback classes that I use for handling callbacks with
I have a set of core, complicated JavaScript data structures/classes that I'd like to
I have been assigned a project to develop a set of classes that act
I have a set of some classes which are all capable of being constructored
I would love to be able to have a common set of Classes in
I have a set of XSDs from which I generate data access classes, stored
I have the following classes: public class Person { public String FirstName { set;
We have set up a system where notifications get sent to a user with
I have set a canvas' background to an image of a company logo. I

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.