473,796 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create generic class constructor

public class BusinessObjectG eneric<EntityTy pe: BusinessObject
where EntityType : BusinessEntity, new() {

public BusinessObjectG eneric<EntityTy pe() {
}

.....
causes error in constructor declaration:

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

How to create generic class constructor ?

Andrus.

May 8 '07 #1
7 38615
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]
- mv*@spam.guard. caspershouse.co m

"Andrus" <ko********@hot .eewrote in message
news:uw******** ******@TK2MSFTN GP02.phx.gbl...
public class BusinessObjectG eneric<EntityTy pe: BusinessObject
where EntityType : BusinessEntity, new() {

public BusinessObjectG eneric<EntityTy pe() {
}

....
causes error in constructor declaration:

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

How to create generic class constructor ?

Andrus.

May 8 '07 #2
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]" <mv*@spam.guard .caspershouse.c omwrote in
message news:um******** ******@TK2MSFTN GP02.phx.gbl...
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.
May 8 '07 #3
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]
- mv*@spam.guard. caspershouse.co m

"Andrus" <ko********@hot .eewrote in message
news:uX******** ******@TK2MSFTN GP06.phx.gbl...
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]" <mv*@spam.guard .caspershouse.c omwrote
in message news:um******** ******@TK2MSFTN GP02.phx.gbl...
>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.

May 8 '07 #4
I'd like to create new entity object in business object constructor:

public BusinessObjectG eneric<EntityTy pe(string param) {

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

Andrus.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
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]
- mv*@spam.guard. caspershouse.co m

"Andrus" <ko********@hot .eewrote in message
news:uX******** ******@TK2MSFTN GP06.phx.gbl...
>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]" <mv*@spam.guard .caspershouse.c omwrote
in message news:um******** ******@TK2MSFTN GP02.phx.gbl...
>>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.

May 8 '07 #5
public BusinessObjectG eneric<EntityTy pe() {
}
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
May 8 '07 #6
Andrus,

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

public BusinessObjectG eneric<TEntityT ypewhere TEntityType : new()
{
public BusinessObjectG eneric(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]
- mv*@spam.guard. caspershouse.co m

"Andrus" <ko********@hot .eewrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
I'd like to create new entity object in business object constructor:

public BusinessObjectG eneric<EntityTy pe(string param) {

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

Andrus.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
>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]
- mv*@spam.guard. caspershouse.co m

"Andrus" <ko********@hot .eewrote in message
news:uX******* *******@TK2MSFT NGP06.phx.gbl.. .
>>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]" <mv*@spam.guard .caspershouse.c omwrote
in message news:um******** ******@TK2MSFTN GP02.phx.gbl...
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.


May 9 '07 #7
"Andrus" <ko********@hot .eeschrieb im Newsbeitrag
news:uw******** ******@TK2MSFTN GP02.phx.gbl...
public class BusinessObjectG eneric<EntityTy pe: BusinessObject
where EntityType : BusinessEntity, new() {

public BusinessObjectG eneric<EntityTy pe() {
}
<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 BusinessObjectG eneric<EntityTy pe: BusinessObject
where EntityType : BusinessEntity, new() {

public BusinessObjectG eneric() {
}

The constructor by itself is not generic.

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

Christof
May 9 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2383
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one or another (could also be 3) different web servers to use these Web Services. The Web Services are identical on all the machines. I tried just changing the URL of the Web Services and cannot make it work. I then decided to create 2 identical web...
2
9868
by: AdawayNoSpam | last post by:
Said that I have the following class Class MyRootClass(Of T) End Class Class MySubClass1(Of T) Inherits MyRootClass(Of T) End Class
4
9175
by: Hyun-jik Bae | last post by:
Is that not allowed to assume generic type numeric type? As far as I've tried, I got an error with the following code: public class AAA<T> { public int Foo(T a) { return a; // error: Cannot implicitly convert type 'T' to 'int' } }
4
2169
by: mojeza | last post by:
I would like to create generic object which will be used for store of single row of DataTable. Lets say I create class as follow: Public Class Participant Public ParticipantID As Int64 Public LastName As String Public FirstName As String End Class then I get a data from the database to DataTable and create array of
8
5383
dmjpro
by: dmjpro | last post by:
hey i m new in Generic Class in java. look at this code carefully .... class GenerericClass<T> { private T d1,d2; public GenericClass() {
10
1940
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that I will need the Generic Class, given there are so many other options. -- cheers,
7
1822
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to create a generic class which will take a command string and I can issue statement like this: Dim rdrCustomers As New Generic_Reader("select * from customers") However, I have not found anyway to do that. It seems that I may need to create some functions instead of class and issue statement like this: Dim rdrCustomers As SqlDataReader = GetDataReader("select * from customers")
10
2963
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a generic list of business objects. I'm able to see that the type is the correct one in the debugger. However when I try to traverse the list using the type I can't compile. The same type variable I've verified as being passed
9
3154
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than the "T" type of the instanced class? ie, public class SomeList<T> { public SomeList(SomeList<TthisSomeList)
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.