473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

'identifier': cannot provide arguments when creating an instance of a variable type

I would like to get this to compile:

public class GenericCtor<T> where T: class, new()
{
public static T MakeIt()
{
return new T(1, 2);
}
}

public class GenericCtorItem
{
int A, B;

public GenericCtorItem(int a, int b)
{
A = a;
B = b;
}
}

The documentation says: "This error occurs if a call to the new
operator on a type parameter has arguments. The only constructor that
can be called using the new operator on an unknown parameter type is a
constructor with no arguments. If you need to call another constructor,
consider using a class type constraint or interface constraint."

Which to me, implies that if use a class type or interface constraint,
this should work:

public class GenericCtor<T> where T: GenericCtorItem, new()
{
public static T MakeIt()
{
return new T(1, 2);
}
}

public class GenericCtorItem
{
int A, B;

public GenericCtorItem(int a, int b)
{
A = a;
B = b;
}
}

But, it doesn't. Is there a way at all to call a constructor for this?

Apr 5 '06 #1
1 5587
No, there isn't a way to do this. Constraints on an interface don't
matter, since interfaces can't have constructors. With class constraints,
any derived class can be used for T as well, and derived classes do not
inherit constructors.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<tb******@storkyak.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
I would like to get this to compile:

public class GenericCtor<T> where T: class, new()
{
public static T MakeIt()
{
return new T(1, 2);
}
}

public class GenericCtorItem
{
int A, B;

public GenericCtorItem(int a, int b)
{
A = a;
B = b;
}
}

The documentation says: "This error occurs if a call to the new
operator on a type parameter has arguments. The only constructor that
can be called using the new operator on an unknown parameter type is a
constructor with no arguments. If you need to call another constructor,
consider using a class type constraint or interface constraint."

Which to me, implies that if use a class type or interface constraint,
this should work:

public class GenericCtor<T> where T: GenericCtorItem, new()
{
public static T MakeIt()
{
return new T(1, 2);
}
}

public class GenericCtorItem
{
int A, B;

public GenericCtorItem(int a, int b)
{
A = a;
B = b;
}
}

But, it doesn't. Is there a way at all to call a constructor for this?

Apr 5 '06 #2

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

Similar topics

8
by: Mark English | last post by:
I'd like to write a Tkinter app which, given a class, pops up a window(s) with fields for each "attribute" of that class. The user could enter values for the attributes and on closing the window...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
8
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA....
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
1
by: Rajiv Das | last post by:
The following code chunk static T CreateControl<T>(string name) where T : new() { new T(name); } results in Compiler Error CS0417
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
5
by: Rain | last post by:
Is there a way for an object to know the identifier which has called it MyClass myClassObjectId = new MyClass(); how can I get the name of the identifier "myClassObjectId" which is calling the...
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.