oillio
oillio

Reputation: 4908

Passing information to a constructor without using a parameter in C#

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.

Upvotes: 1

Views: 1186

Answers (4)

GregL
GregL

Reputation:

How about doing something like that?

using System.Collections.Generic;

class OuterClass
{
    private class BadClass
    {
        private int _data;

        public BadClass()
        {
            _data = 0;

        }

        public int Data
        {
          get
          {
             return _data;
          }
          set
          {
             _data = value;
          }
        }

    }

    void SomeMethod()
    {
        List<BadClass> a = new List<BadClass>() 
        { 
          new BadClass() { Data = 7 }, 
          new BadClass() { Data = 9 } 
        };
    }
}

Upvotes: 1

Rob
Rob

Reputation: 3516

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>.

Upvotes: 2

abelenky
abelenky

Reputation: 64672

Can you give BadClass an initialization method?

private class BadClass
{
    private int Data;

    public BadClass()
    {
        Data = 0;
        ...
    }

    public void Init(int dataValue)
    {
        Data = dataValue;
    }
}

Then when you create one it is:

BadClass myInst = new BadClass(); // void constructor, as you require.
myInst.Init(5);  // Set the value to 5.

Upvotes: 1

John Saunders
John Saunders

Reputation: 161773

When you create the List<BadClass>, you are not creating any BadClass instances. Go ahead and create a that way, but when you create a BadClass instance to add to it, that's when you call your parameterized constructor:

List<BadClass> a = new List<BadClass>();
a.Add(new BadClass(1));
a.Add(new BadClass(2));

By the way, having the construction of a BadClass instance depend on which OuterClass is creating it is a bit of a code smell. What you you trying to accomplish?

Upvotes: 6

Related Questions