How to create generic class constructor

A

Andrus

public class BusinessObjectGeneric<EntityType> : BusinessObject
where EntityType : BusinessEntity, new() {

public BusinessObjectGeneric<EntityType> () {
}

.....


causes error in constructor declaration:

Error 1 Invalid token '(' in class, struct, or interface member declaration

How to create generic class constructor ?

Andrus.
 
N

Nicholas Paldino [.NET/C# MVP]

Andrus,

You can't create a generic class constructor. If you want to take
advantage of parameterized types in the constructor, then you will have to
take the type in the class declaration level.

Hope this helps.
 
A

Andrus

Why this limitation exists ?

I want to assing values to some properties in constructor.
Any idea how this can be done ?

Andrus.
 
N

Nicholas Paldino [.NET/C# MVP]

Andrus,

What kind of properties are you trying to assign to in the constructor?

What would you like to be able to do if the compiler let you?
 
A

Andrus

I'd like to create new entity object in business object constructor:

public BusinessObjectGeneric<EntityType> (string param) {

Entity = new <EntityType> ();
if (Entity!=null)
Entity.propery = param;
}

Andrus.


Nicholas Paldino said:
Andrus,

What kind of properties are you trying to assign to in the constructor?

What would you like to be able to do if the compiler let you?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrus said:
Why this limitation exists ?

I want to assing values to some properties in constructor.
Any idea how this can be done ?

Andrus.
 
M

Mark Wilden

public BusinessObjectGeneric said:

Here, you're trying to parameterize a method by a type. As others have said,
that can't be done for a constructor. The class, on the other hand, can and
is parameterized by a type. That's all you need.

///ark
 
N

Nicholas Paldino [.NET/C# MVP]

Andrus,

In this case, you would have to have EntityType be in the class itself,
and then do this:

public BusinessObjectGeneric<TEntityType> where TEntityType : new()
{
public BusinessObjectGeneric(string param)
{
TEntityType entity = new TEntityType();
}
}

Now, the thing here is that the property you want to set has to come
from a base class/interface which you specify in the constraints, or you
have to access it through reflection.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrus said:
I'd like to create new entity object in business object constructor:

public BusinessObjectGeneric<EntityType> (string param) {

Entity = new <EntityType> ();
if (Entity!=null)
Entity.propery = param;
}

Andrus.


Nicholas Paldino said:
Andrus,

What kind of properties are you trying to assign to in the
constructor?

What would you like to be able to do if the compiler let you?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrus said:
Why this limitation exists ?

I want to assing values to some properties in constructor.
Any idea how this can be done ?

Andrus.

in message Andrus,

You can't create a generic class constructor. If you want to take
advantage of parameterized types in the constructor, then you will have
to take the type in the class declaration level.
 
C

Christof Nordiek

Andrus said:
public class BusinessObjectGeneric<EntityType> : BusinessObject
where EntityType : BusinessEntity, new() {

public BusinessObjectGeneric<EntityType> () {
}
How to create generic class constructor ?
If by generic class constructor you mean a constructor of a generic class:
simply omit the type parameter list of the constructor:

public class BusinessObjectGeneric<EntityType> : BusinessObject
where EntityType : BusinessEntity, new() {

public BusinessObjectGeneric() {
}

The constructor by itself is not generic.

Yes, the error message could be clearer in this case.

Christof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top