GroZZleR

Hey all,

I have a class, Entity, that accepts a parameter in its constructor. For simplicity's sake, let's say it looks like this:
Code Snippet

public class Entity
{
int number;

public Entity(int number)
{
this.number = number;
}
}


I'm trying to have a factory create these objects through a generic method:
Code Snippet

public T Create<T>(int arg) where T : Entity
{
return new T(arg);
}


But I get the error:
'T': cannot provide arguments when creating an instance of a variable type

Is there any way to accomplish this without resorting to a (very ugly, imo) Initialize method or something to that degree

Thanks for the help.



Re: Visual C# Language Instantiating a generic object without a default constructor?

Thomas Danecker

Why do you need this method If you know the type anyway, you may just use "new" to create the object instead of this method.

Instead of calling "Create<ConcreteEntity>(value);" you may use "new ConcreteEntity(value);" and you'll have no problems. It doesn't make sense to use a dedicated method for this purpose though the the CLR offers "new".






Re: Visual C# Language Instantiating a generic object without a default constructor?

GroZZleR

I'm writing a performance critical app (a game) and I want to re-use old objects that are considered "dead" instead of creating new objects every time. That's not shown in the example for the sake of simplicity.





Re: Visual C# Language Instantiating a generic object without a default constructor?

Sean Fowler

From what I see you don't need a generic method. Generic methods are useful where T can be a number of different types, but in your case it will always be "Entity". So you can just use the type Entity insead.

Sean




Re: Visual C# Language Instantiating a generic object without a default constructor?

GroZZleR

Yes, it will always be an Entity or a DERIVED CLASS from Entity. I have a very large hierarchy of objects below it.

Can we please stop making assumptions about why I need to do it, or ways to do it more simply I need this solution because objects need to be re-used when they're removed from the game world but not removed from memory, to prevent garbage collection hiccups and to generally improve performance.

Is it, or is it not, possible to instantiate an object with parameters It's really just that simple.





Re: Visual C# Language Instantiating a generic object without a default constructor?

Alan M.

GroZZleR wrote:

I need this solution because objects need to be re-used when they're removed from the game world but not removed from memory, to prevent garbage collection hiccups and to generally improve performance..




I can not think of a way, and don't think it is possible, to instantiate a generic with an integer.

However, I think you may have a different problem.

Say I have an "Entity" object that I have created.

SubEntity temp = new SubEntity(1);

then later...

temp = Create<SubEntity>(2);

I would still have garbage collection here wouldn't I In your code you are calling "new" which allocates more memory, and you will have lost your reference to the old object and it will still be garbage collected. If you want to avoid garbage collection it seems like you would need to have a method that reassigns any member variable in your object to some default or new data, without actually calling new on anything.




Re: Visual C# Language Instantiating a generic object without a default constructor?

GroZZleR

Thanks for the concise answer. I guess I'll have to use an Initilization method, which seems really lame. Stick out tongue

Your example is correct, that would cause garbage collection. The objective is to never create new entities by using "new" directly, but always by requesting them through the Factory, which will (not in the example) go through a list that it keeps of unused entities and hand out a reference to that. If one isn't available, then it will create a new instance with new.





Re: Visual C# Language Instantiating a generic object without a default constructor?

Alan M.

perhaps you should implement an interface and require that your generic implements it


public interface IReusable
{
void Reset(int newId);
}


then


public T Create<T>(int id) where T : IReuseable



....



edit: Actually if you already have Entity.. I'd just make that Reset method an abstract method.

public class Entity
{

int id;


public Entity(int id)
{
this.id = id;
}

public abstract void Reset(int newId);
}




Re: Visual C# Language Instantiating a generic object without a default constructor?

GroZZleR

Just for the record, that integer is purely an example of a parameter. No actual ID is being handed to the entities like that. Smile

Thanks for the advice. That looks like the cleanest solution without being able to call a non-default constructor.





Re: Visual C# Language Instantiating a generic object without a default constructor?

Thomas Danecker

You can use the Activator class:

Code Snippet

public T Create<T>(int arg) where T : Entity

{

return (T)Activator.CreateInstance(typeof(T), arg);

}

Sorry that I didn't thought early on this.