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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:56:28+00:00 2026-05-11T16:56:28+00:00

Bellow is simplified version of the code I have: public interface IControl<T> { T

  • 0

Bellow is simplified version of the code I have:

public interface IControl<T>
{
    T Value { get; }
}

public class BoolControl : IControl<bool>
{
    public bool Value
    {
        get { return true; }
    }
}

public class StringControl : IControl<string>
{
    public string Value
    {
        get { return ""; }
    }
}
public class ControlFactory
{
    public IControl GetControl(string controlType)
    {
        switch (controlType)
        {
            case "Bool":
                return new BoolControl();
            case "String":
                return new StringControl();
        }
        return null;
    }
}

The problem is in GetControl method of ControlFactory class. Because it returns IControl and I have only IControl<T> that is a generic interface. I cannot provide T because in Bool case it’s going to bool and in String case it’s going to be string.

Any idea what I need to do to make it work?

  • 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-11T16:56:28+00:00Added an answer on May 11, 2026 at 4:56 pm

    Just derive IControl<T> from IControl.

    public interface IControl<T> : IControl
    {
        T Value { get; }
    }
    

    UPDATE

    If I missunterstood you, and you don’t want a non-generic interface, you will have to make the method GetControl() generic, too.

    public IControl<T> GetControl<T>()
    {
        if (typeof(T) == typeof(Boolean))
        {
            return new BoolControl(); // Will not compile.
        }
        else if (typeof(T) == typeof(String))
        {
            return new StringControl(); // Will not compile.
        }
        else
        {
            return null;
        }
    }
    

    Now you have the problem that the new controls cannot be implicitly casted to IControl<T> and you would have to make this explicit.

    public IControl<T> GetControl<T>()
    {
        if (typeof(T) == typeof(Boolean))
        {
            return new (IControl<T>)BoolControl();
        }
        else if (typeof(T) == typeof(String))
        {
            return (IControl<T>)new StringControl();
        }
        else
        {
            return null;
        }
    }
    

    UPDATE

    Changed the cast from as IControl<T> to (IControl<T>). This is prefered because it will cause an exception if there is a bug while as IControl<T> silently returns null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following C++ code (simplified version): class Shape { bool isCircle =
I have two data structure classes (this is a simplified version of my code)
This is a simplified version of the original problem. I have a class called
This is a simplified version of what I want to do. Basically I have
Here's a simplified version of my needs. I have a program where every B
The code below is a simplified version of the pattern my project is using.
Below is a simplified version of SQL script I have. print @RowNum always shows
Given the code bellow, how do I style the radio buttons to be next
I have a simple form with some plain html input like bellow using ASP.NET
I am making a simplified version of the ball physics app found at this

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.