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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:09:27+00:00 2026-05-21T03:09:27+00:00

I have an (existing) typed class of items: Items<T> T Value { get; }

  • 0

I have an (existing) typed class of items:

Items<T>
    T Value { get; }

T can be double, string or int.

I then have a class that has to hold several instances of Items. Within a single instance of this class, T is always the same. As it stands, the type actually contained is determined by a property and the container is not typed:

Data
    DataType { get; set; }
    Items<double>
        double Value;
    Items<string> 
        // ... and so on. Nasty stuff.

Ideally, of course, this would be

Data<T>
    Items<T>
        T value

Data instances are created from scratch in code, and can be loaded from a database. So of course a factory would be in our future, but what is the return type of the Create method?

Even worse, I need this:

DataCollection
    // HERE'S THE PAIN: What's the type here?
    List of Data<> instances with differing types

foreach (? data in someDataCollection)
    if (thetypeof data is double)
        doSomething();
    else
        doSomethingElse();

Now, I can solve this, but I can’t see a CLEAN way to solve this.

My first issue is the declaration of DataCollection. What is the type of the list? List<object>, so it can hold Data<double> and Data<string>?

  • 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-21T03:09:28+00:00Added an answer on May 21, 2026 at 3:09 am

    There actually is a clean way to solve this; you can use a Dictionary with keys of the data type and values which are of type generic Func<> . You then pass the type to your create method, which then looks up the Func<> to use in the Dictionary based on the type, and invokes that Func<> to create or process your object.

    Since I am working from pseudo code, basically it would look something like the below; you can play with it and modify it to get it to serve your needs, but this is the basic idea.

    First, create a parent class for all data objects; note that this class has a lookup dictionary for functions to invoke on various types, and note that it is abstract:

    public abstract class Data
    {
    
        // A Lookup dictionary for processing methods
        // Note this the functions just return something of type object; specialize as needed
        private static readonly IDictionary<Type, Func<object, Data>> _processFunctions = new Dictionary
            <Type, Func<object, Data>>()
             {
                 {typeof(int), d => { return doSomethingForInt( (Data<int>) d); }},
                 {typeof(string), d => { return doSomethingForString( (Data<string>) d); }},
                 {typeof(double), d => { return doSomethingForDouble( (Data<double>) d); }},
    
             };
    
        // A field indicating the subtype; this will be used for lo
        private readonly Type TypeOfThis;
    
        protected Data(Type genericType)
        {
            TypeOfThis = genericType;
        }
    
        public Data Process()
        {
            return _processFunctions[this.TypeOfThis](this);
        }
    
    }
    

    Now subclass Data with a generic type that can be instantiated:

    class Data<T> : Data
    {
    
        // Set the type on the parent class
        public Data() : base(typeof(T))
        {
        }
    
        // You can convert this to a collection, etc. as needed
        public T Items { get; set; }
    
        public static Data<T> CreateData<T>()
        {
            return new Data<T>();
        }
    }
    

    You can then create a DataCollection class using the parent type. Note the ProcessData() method; all it does now is loop over the elements and call Process() on each one:

    class DataCollection
    {
        public  IList<Data> List = new List<Data>();
    
        public void ProcessData()
        {
            foreach (var d in List)
            {
                d.Process();
            }
        }
    
    }
    

    …and you’re all set! Now you can invoke your DataCollection with different types of Data:

    DataCollection dc = new DataCollection();
    
    dc.List.Add(new Data<int>());
    dc.List.Add(new Data<string>());
    dc.List.Add(new Data<double>());
    
    
    dc.ProcessData();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have existing code that uses CMNewProfileSearch to find then iterate over the color
I have existing Linux shared object file (shared library) which has been stripped. I
I have existing java code and need to create Design Document based on that.
I have an existing database(myDatabse) that I created with Microsoft Sql Managenent studio and
I have a C# class which needs to process a sequence of items (
I have the following interfaces that are part of an existing project. I'd like
I have a class Item and a class ItemAttribute, where Item has a property
I have a C# class called SmallClass. I have an existing list myList containing
I have a model that contains a collection, such as this: class MyModel {
I have an existing event heavy page that I am trying to add typeahead

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.