473,569 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics: Type parameter with type parameter

Hi to All

I would like to do something similar:

class Class1<T1>
{
}

class Class2<T2, T1where T2: Class1<T1>, new()
{
public void M()
{
T2<T1x;
}

Error: The type parameter 'T2' cannot be used with type arguments.

How to accomplish this task?

thx for help
Aug 2 '06 #1
7 5452
Hi John,
john schrieb:
Hi to All

I would like to do something similar:

class Class1<T1>
{
}

class Class2<T2, T1where T2: Class1<T1>, new()
{
public void M()
{
T2<T1x;
The type parameter T2 already represents the type (or subtype)
Class1<T1>, not Class1<>.
When writing T2<T1C# interpretes this like Class1<T1><T1>, which will
obviously not work.

Try just typing T2 x;
}

Error: The type parameter 'T2' cannot be used with type arguments.

How to accomplish this task?

thx for help

HTH,
Stefan
Aug 2 '06 #2
Well in your code, T2 is already defined as a discreet " : Class1<T1>", and
so there isn't much left to define; are you sure you don't just need T2 x =
new T2();?

If not, can you clarify how you intend to use it? Perhaps with more
penetrable class names so we can see what you are trying to do?

Marc
Aug 2 '06 #3
Hi thx for the fast answer,

A would like to use a type and its type parameter both as type parameter in
a third class

Sample:

class NonGenericClass
{
}

class1<Twhere T: NonGenericClass
{
}
class2<Twhere T: NonGenericClass
{
}

class3<T1, T2where ?what to write here?
{
public void M()
{
T1<T2x = new T1<T2>();
}
}
and use this somewhere:

class3<class1, NonGenericClass a;
class3<class2, NonGenericClass b
....

thx for help


"Marc Gravell" <ma**********@g mail.comaz alábbiakat írta a következő
hírüzenetben: O9************* *@TK2MSFTNGP03. phx.gbl...
Well in your code, T2 is already defined as a discreet " : Class1<T1>",
and so there isn't much left to define; are you sure you don't just need
T2 x = new T2();?

If not, can you clarify how you intend to use it? Perhaps with more
penetrable class names so we can see what you are trying to do?

Marc

Aug 2 '06 #4
As just stated above, use:

class3<T1, T2where T1 : Class1<T2>, new()
{
public void M()
{
T1 x = new T1();
}
}

class3<class1, NonGenericClass a
class3<class2, NonGenericClass b

I assume class2 is inheriting from class1...

HTH,
Stefan
john schrieb:
Hi thx for the fast answer,

A would like to use a type and its type parameter both as type parameter in
a third class

Sample:

class NonGenericClass
{
}

class1<Twhere T: NonGenericClass
{
}
class2<Twhere T: NonGenericClass
{
}

class3<T1, T2where ?what to write here?
{
public void M()
{
T1<T2x = new T1<T2>();
}
}
and use this somewhere:

class3<class1, NonGenericClass a;
class3<class2, NonGenericClass b
...

thx for help


"Marc Gravell" <ma**********@g mail.comaz alábbiakat írta a következő
hírüzenetben: O9************* *@TK2MSFTNGP03. phx.gbl...
>>Well in your code, T2 is already defined as a discreet " : Class1<T1>",
and so there isn't much left to define; are you sure you don't just need
T2 x = new T2();?

If not, can you clarify how you intend to use it? Perhaps with more
penetrable class names so we can see what you are trying to do?

Marc


Aug 2 '06 #5
You can't; there is no such thing as class1 nor class2; only class1<Tand
class2<T>

You could use, for instance class3<T>, and then

class3<class1<N onGenericClass> >
or
class3<class2<S omethingInherit edFromNonGeneri cClass>>

however, you cannot have a condition in the form you have expressed, as it
would make no sense as the condition could not possibly be evaluated in any
way at compile time. You could perhaps use some kind of interface, but
without concrete, meaningful examples of what you are trying to do it is
very hard to answer. By concrete, I mean something like below - annd *even
then* I'm not sure why I would want to do this... what you are trying to
achieve is probably more important than how you are trying to do it, as I
suspect there is a better way...

Marc

using System;
using System.Collecti ons.Generic;
using System.Collecti ons.ObjectModel ;
abstract class Animal {}
class Dog : Animal {}
class AnimalCollectio n<T: Collection<Twhe re T : Animal {}
class AnimalList<T: List<Twhere T : Animal {}
class AnimalWalker<TA nimalType, TCollectionType >
where TCollectionType : IEnumerable<TAn imalType>
where TAnimalType : Animal{ }

class Program {
static void Main()
{
AnimalWalker<Do g, AnimalList<Dog> dogWalker = new
AnimalWalker<Do g,AnimalList<Do g>>();
AnimalWalker<An imal, AnimalCollectio n<Animal>petWal ker = new
AnimalWalker<An imal, AnimalCollectio n<Animal>>();

}
}
Aug 2 '06 #6
Hello John,

From what I can see I think there are a couple of things you should look
at. I have modified your code to show what I am talking about. One of the
things I found odd was that you were trying to use T2 in any form like this.
The purpose of constraints is to be able to (1) enforce that the correct
type is passed in and (2) hopefully to work with the exposed properties and
methods of that type. Two, that by not having either a member variable that
is assigned during the constructor calll or parameter passed in via a method
or property, the value of T2 is really nothing so you won't really be able
to use it.

public class Class1<T1>
{
Class1(){}

private int myVar;

public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}

}

public class Class2<T2, T1where T2: Class1<T1>, new()
{
//option 1 to have member variables and assign them in a constructor
T2 t2;
T1 t1;

Class2(T2 t2, T1 t1)
{
this.t1 = t1;
this.t2 = t2;
}

//option 2 to have the variable passed in.
public void M(T2 myT2)
{
myT2.MyProperty = 100;
}
}

Adam Calderon [C# MVP]
http://blogs.interknowlogy.com/adamcalderon
Hi to All

I would like to do something similar:

class Class1<T1>
{
}
class Class2<T2, T1where T2: Class1<T1>, new()
{
public void M()
{
T2<T1x;
}
Error: The type parameter 'T2' cannot be used with type arguments.

How to accomplish this task?

thx for help

Aug 2 '06 #7
.... which will not work unless you type

class3<class1<N onGenericClass> , NonGenericClass a

Sorry, but I see no chance to get aroud this inconvenience in typing.
Automatic type determination by C# will only be performed on method
calls, not for classes on variable declaration...

HTH,
Stefan
Stefan L schrieb:
As just stated above, use:

class3<T1, T2where T1 : Class1<T2>, new()
{
public void M()
{
T1 x = new T1();
}
}

class3<class1, NonGenericClass a
class3<class2, NonGenericClass b

I assume class2 is inheriting from class1...

HTH,
Stefan
john schrieb:
>Hi thx for the fast answer,

A would like to use a type and its type parameter both as type
parameter in a third class

Sample:

class NonGenericClass
{
}

class1<Twher e T: NonGenericClass
{
}
class2<Twher e T: NonGenericClass
{
}

class3<T1, T2where ?what to write here?
{
public void M()
{
T1<T2x = new T1<T2>();
}
}
and use this somewhere:

class3<class 1, NonGenericClass a;
class3<class 2, NonGenericClass b
...

thx for help


"Marc Gravell" <ma**********@g mail.comaz alábbiakat írta a következő
hírüzenetben : O9************* *@TK2MSFTNGP03. phx.gbl...
>>Well in your code, T2 is already defined as a discreet " :
Class1<T1>" , and so there isn't much left to define; are you sure you
don't just need T2 x = new T2();?

If not, can you clarify how you intend to use it? Perhaps with more
penetrable class names so we can see what you are trying to do?

Marc


Aug 2 '06 #8

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

Similar topics

11
3980
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I looked at the files in the Gyro download but I couldn't find any mention of constraints. Can anyone enlighten me what the current status is and what we...
10
2586
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results other than straining our relationship... I just downloaded Visual C# Express Edition to mess with .NET 2.0 generics. Being a numerically...
8
1191
by: Ashish | last post by:
This is just a thought, may be its not possible , but is it possible to use generics in method declarations as return type arguments.. for example public <T> Load(T value) { //method body }
17
1921
by: atgraham | last post by:
Here is the "lead C# architect" attempting to impugn C++ templates (bottom of the page). http://www.artima.com/intv/generics2.html (bottom of the page) (Full article begins at http://www.artima.com/intv/generics.html) I think every employee at Micrsoft must be required to take a class in "FUD marketing" before speaking with anyone outside...
10
1370
by: Dan Holmes | last post by:
I created a class that accepts a type parameter. I then want to put all of those into one list<>. List<Parameter<object>> lp = new List<Parameter<object>>(); Parameter<string> licenceKey = new Parameter<string>(); licenceKey.Name = "LICENSEKEY"; licenceKey.Section = "General"; licenceKey.Value = "IVS"; //default 3 user key...
1
2426
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes "ThrowInConstructor" and "ThrowInFoo". First one throws "MyOwnException" in constructor, second one in "Foo()" method. There is a "GenericCatch" generics class...
19
1523
by: Barry Mossman | last post by:
Hi, if I have a generic class such as: public class MyGroup<T> : Collection<MyChildClass> and from one of it's methods I want to pass a reference to myself to the following method in another class: public void OnValidateChanged(MyGroup<MyChildClass> group) I am trying:
4
2798
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool RuleDelegate<T>(T item); In my class my goal is to use a generic list collection to contain my generic delegates. This will allow me to pass this List to...
6
3059
by: CassioT | last post by:
Hi. I want to create a base form class with a generic parameter. public BaseForm<T: Form {} public MyForm : BaseForm<MyType> {} The problem here is that the inherited form doesn't work in the visual studio designer but the execution is perfect. Without the generic
5
2232
by: teel | last post by:
Hi there, I'm trying to apply "less than" and "more than" operators on the Generics (class template-like) type objects. Below is the code of my class representing a parameter that can be any type, but only if it's float or int the comparison results in true/false-like result, else its INVALID. I get error: Operator '<' cannot be applied to...
0
7693
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...
0
7605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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. ...
0
8118
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...
1
7665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5501
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...
0
5217
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
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.