PC Review


Reply
Thread Tools Rate Thread

How to create generic class constructor

 
 
Andrus
Guest
Posts: n/a
 
      8th May 2007
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.

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      8th May 2007
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.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Andrus" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Andrus
Guest
Posts: n/a
 
      8th May 2007
Why this limitation exists ?

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

Andrus.

"Nicholas Paldino [.NET/C# MVP]" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      8th May 2007
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 Removed)

"Andrus" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Why this limitation exists ?
>
> I want to assing values to some properties in constructor.
> Any idea how this can be done ?
>
> Andrus.
>
> "Nicholas Paldino [.NET/C# MVP]" <(E-Mail Removed)> wrote
> in message news:(E-Mail Removed)...
>> 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.

>



 
Reply With Quote
 
Andrus
Guest
Posts: n/a
 
      8th May 2007
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 [.NET/C# MVP]" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
> 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 Removed)
>
> "Andrus" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Why this limitation exists ?
>>
>> I want to assing values to some properties in constructor.
>> Any idea how this can be done ?
>>
>> Andrus.
>>
>> "Nicholas Paldino [.NET/C# MVP]" <(E-Mail Removed)> wrote
>> in message news:(E-Mail Removed)...
>>> 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.

>>

>
>


 
Reply With Quote
 
Mark Wilden
Guest
Posts: n/a
 
      8th May 2007
> public BusinessObjectGeneric<EntityType> () {
> }


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


 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      9th May 2007
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 Removed)

"Andrus" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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 [.NET/C# MVP]" <(E-Mail Removed)> wrote
> in message news:%(E-Mail Removed)...
>> 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 Removed)
>>
>> "Andrus" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Why this limitation exists ?
>>>
>>> I want to assing values to some properties in constructor.
>>> Any idea how this can be done ?
>>>
>>> Andrus.
>>>
>>> "Nicholas Paldino [.NET/C# MVP]" <(E-Mail Removed)> wrote
>>> in message news:(E-Mail Removed)...
>>>> 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.
>>>

>>
>>

>



 
Reply With Quote
 
Christof Nordiek
Guest
Posts: n/a
 
      9th May 2007
"Andrus" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> public class BusinessObjectGeneric<EntityType> : BusinessObject
> where EntityType : BusinessEntity, new() {
>
> public BusinessObjectGeneric<EntityType> () {
> }

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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a generic class re-create itself with another type tadmill@yahoo.com Microsoft C# .NET 8 18th Jun 2008 03:12 PM
Calling base class default constructor when the base is a generic... SammyBar Microsoft C# .NET 3 16th Jan 2006 06:36 PM
InitializeComponent is generating the wrong code for my custom class. Calling it's base class constructor jrhoads23@hotmail.com Microsoft Dot NET Framework Forms 0 1st Feb 2005 07:10 PM
Calling a struct constructor in a class constructor body Karl M Microsoft VC .NET 4 19th Dec 2004 01:21 PM
Initializing base class portion of a derived class in a constructor J.J. Feminella Microsoft C# .NET 2 9th Apr 2004 10:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:21 PM.