473,241 Members | 1,562 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,241 software developers and data experts.

Trouble understanding compiler message on generic where clause

I have this method declaration:

private SomeType getSomething<T>( T spec ) where T: String, MemoryStream

The compiler error I get is:

'string' is not a valid constraint. A type used as a constraint must be
an interface, a non-sealed class or a type parameter.

I don't understand what is meant by "type parameter" in this context. I
understand a "type parameter" to be my "T" so I don't understand how a
constraint can be the same thing...
Jan 19 '06 #1
3 1686
Hello Brad,

For one, string is sealed so that's why it's not working.

Second, you could do something like this:

public class AClass<T>
{
private void Do<d>(d val) where d : T { }
}
--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net
I have this method declaration:

private SomeType getSomething<T>( T spec ) where T: String,
MemoryStream

The compiler error I get is:

'string' is not a valid constraint. A type used as a constraint must
be an interface, a non-sealed class or a type parameter.

I don't understand what is meant by "type parameter" in this context.
I understand a "type parameter" to be my "T" so I don't understand how
a constraint can be the same thing...

Jan 19 '06 #2
Brad Wood <bradley|.wood|@ndsu|.edu> wrote:
I have this method declaration:

private SomeType getSomething<T>( T spec ) where T: String, MemoryStream

The compiler error I get is:

'string' is not a valid constraint. A type used as a constraint must be
an interface, a non-sealed class or a type parameter.

I don't understand what is meant by "type parameter" in this context. I
understand a "type parameter" to be my "T" so I don't understand how a
constraint can be the same thing...


T is the type parameter, and "where T : String, MemoryStream" is the
constraint. However:

1) You can't have a constraint against two classes in the same way as
you can't derive from two classes

2) You can't constrain a type to have a "minimal base type" which is
sealed (like string)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 19 '06 #3
Patrik Löwendahl [C# MVP] wrote:
Second, you could do something like this:

public class AClass<T> {
private void Do<d>(d val) where d : T { }
}


Got it; a where constraint can include a reference to a type parameter
other than itself. Thanks.
Jan 20 '06 #4

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

Similar topics

6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
59
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
0
by: webmaster | last post by:
Pardon my being a total C# noob. I'm trying to take apart the dotNet Time Tracker dotNet C# starterkit sample application and replicate a part of the code. At a high level, I have a very...
0
by: James Wong | last post by:
Hi everybody, I'm facing a serious trouble relating to GDI+ generic error. The error message is "A Generic error occured in GDI+" and the following information is stored in Excepton object:...
14
by: James Wong | last post by:
Hi everybody, I'm facing a serious trouble relating to GDI+ generic error. The error message is "A Generic error occured in GDI+" and the following information is stored in Excepton object:...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
3
by: Greg Corradini | last post by:
Hello, I'm trying to perform a simple insert statement into a table called Parcel_Test (see code below). Yet, I get an error message that I've never seen before (see traceback below). I've tried...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.