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

The Archive Base Latest Questions

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

I have backed myself into a a bit of a corner. Here is a

  • 0

I have backed myself into a a bit of a corner. Here is a simplified version of what I have (in C#):

class OuterClass
{
    private class BadClass
    {
        private int Data;

        public BadClass()
        {
            Data = 0;

            ...
        }
    }

    T Build<T>(Object Input)
    {
        T x = new T();
        ...
        return x;
    }

    void SomeMethod()
    {
        BadClass a = Build<BadClass>(anObject);
        ...
    }
    ...
}

The problem I have is that I now must change the initial value of Data depending on the instance of OuterClass that is creating the BadClass instance. Normally I would simply use a BadClass constructor that takes a parameter:

public BadClass(int Data)
{
    this.Data = Data;
    ...
}

But I use BadClass in several generic types so I must have a parameterless BadClass constructor. Too bad I can’t do something like this:

Build<BadClass(5)>(anObject);

So, how do I give a constructor run-time information when I can’t pass parameters into the constructor?
Is it possible to create a runtime instance of a BadClass type, give it the information it needs, and then use that in my generic types?

EDIT: I used List as an example generic type but that didn’t fully express the depth of my dilemma…
I can create a test in Build to call an init function if I am working with a BadClass, but that is very hacky. I am hoping to find a slightly less ugly way to go about it.

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

    If you need to have multiple BadClass types that are variations of generics, you can do so by changing your inheritance tree:

    class OuterClass {
        private class BadClassBase {
            // whatever BadClass does 
        }
        private class BadClass : BadClassBase {
            public BadClass(T item) {
                ...
            }
        }
    }
    

    Not sure if this is what you’re going for but you can then create your List<BadClassBase>.

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

Sidebar

Related Questions

I've kind of backed myself into a corner here. I have a series of
I seem to have backed myself up into a corner with this. I'm sure
So I've backed myself into a corner - I wanted an application or command
Ok, this is the situation. I have backed up my database by going into
I have an NSTextView backed by a text system I put together myself, along
Here is my problem in C# : I have the following classes: public class
I'm still on the learning curve with rails, and seem to have backed myself
I have a javascript backed 'print' button on my webpage where the user can
We have a database that is backed up weekly in simple mode. Yesterday, we
I have a UITableView which is backed by a NSFetchedResultsController . I would like

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.