473,666 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Generic Classes as Type Parameters

class A<Ta>
{
public A() { }
}

class B<TA, Ta>
where TA : A<Ta>
{
public B() { }
}

class Program
{
static void Main(string[] args)
{
B<A<int>, int> var = new B<A<int>, int>();

//how I'd like to do it
//B<A<int> > = new B<A<int> >();
}
}

Is there some combination of syntax that'll let me declare a variable
of type B as I'd like to do it? It's a bit clunky having to say that I
want int twice.

Nov 23 '05 #1
3 2235
Levi,

No, there is not a way to do it. You will have to declare it like that
on the type level every time. Type parameters on the method level can be
implied, but at the type level (except when indicating the type in static
methods), they can not be.

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

"Levi" <pi*******@bell south.net> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
class A<Ta>
{
public A() { }
}

class B<TA, Ta>
where TA : A<Ta>
{
public B() { }
}

class Program
{
static void Main(string[] args)
{
B<A<int>, int> var = new B<A<int>, int>();

//how I'd like to do it
//B<A<int> > = new B<A<int> >();
}
}

Is there some combination of syntax that'll let me declare a variable
of type B as I'd like to do it? It's a bit clunky having to say that I
want int twice.

Nov 23 '05 #2
Levi,

One other thing, you can always declare a type alias with the using
statement, and then use that. At the top of your file, you can do:

using MyGenericB = B<A<int>, int>;

Then, in your code, you can do:

MyGenericB var = new MyGenericB();

And it will compile fine.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Levi" <pi*******@bell south.net> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
class A<Ta>
{
public A() { }
}

class B<TA, Ta>
where TA : A<Ta>
{
public B() { }
}

class Program
{
static void Main(string[] args)
{
B<A<int>, int> var = new B<A<int>, int>();

//how I'd like to do it
//B<A<int> > = new B<A<int> >();
}
}

Is there some combination of syntax that'll let me declare a variable
of type B as I'd like to do it? It's a bit clunky having to say that I
want int twice.

Nov 23 '05 #3
I don't have the ability to test this here, but this should work:

B <A<Ta>, Ta> CreateB<Ta>()
{
return new B<A<Ta>, Ta>()
}

static void Main(string[] args)
{
B<A<int>, int> var = CreateB<int>();
}
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
"Levi" <pi*******@bell south.net> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
class A<Ta>
{
public A() { }
}

class B<TA, Ta>
where TA : A<Ta>
{
public B() { }
}

class Program
{
static void Main(string[] args)
{
B<A<int>, int> var = new B<A<int>, int>();

//how I'd like to do it
//B<A<int> > = new B<A<int> >();
}
}

Is there some combination of syntax that'll let me declare a variable
of type B as I'd like to do it? It's a bit clunky having to say that I
want int twice.

Nov 23 '05 #4

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

Similar topics

1
3997
by: Johnas Owan | last post by:
Hi! I'd like to build xml out of some c# classes with the XMLSerializer. My problem is: I have an envelope and want to fill it with different types of sub-nodes. But I don't like to build the envelope class for each subsystem class again and again. Instead the type of sub-node shall be choosen during runtime. Is there a way to use class inheritance or something like that?
3
2488
by: Scottie_do | last post by:
I'm considering switching to C# and using VS2005, but I'd like to know if I can have a list of values at runtime and then specify (at runtime) what it's value type is. For example, I would have this in my Multi-Dimensional Array named people Then, I would then like to say: new Person<People.ToString>(); Where ToString() would output a built in datatype or a struct/object that I
3
2757
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some dead ends. I'm posting this to get some feedback on wether I'm going in the right direction, and at the same time hopefully save others from going through the process.
2
15681
by: john | last post by:
Hi to All, Suppose I would like to implement a generic class with three type parameters: class UniversalClass<TA, TB, TC>() where TA: A, new() where TB: B, new() where TC: C, new()
7
1853
by: Brad Wood | last post by:
The following works fine; passing a type to CreateInstance: private Type _next; BaseWizardForm nextForm = (BaseWizardForm)Activator.CreateInstance( _next ); So why doesn't this work (error = _next' is a 'field' but is used like a 'type'): BaseWizardForm nextForm = (BaseWizardForm)Activator.CreateInstance<_next>();
5
11118
by: Torben Laursen | last post by:
I am writing a COM in C# using visual studio 2005 and VSTO. Inside the code I use some support classes that are generic but they are not used in the inferface of the COM. However I still get a number of warnings from the compiler like: "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets : warning : Type library exporter warning processing 'Utility.Array1D`1, Test2003'. Warning: Type library exporter encountered a...
1
2544
by: Hoss | last post by:
Discovered this today after alot of head-scratching, just thought I would toss it out into cyberspace in hopes that it would help someone. This of course, will not compile public class Woot`Class - notice the backtick { }
2
1982
by: yogb21 | last post by:
Hello ! I want to know...can I write a method inside C# class which has generic input parameters and generic return type. If yes..can someone help me with such method declaration and method usage. Here by 'Generic' I mean to say the return type and input parameters can be anything integer, string, array, object... Thank You and Best Regards !
1
2456
by: raylopez99 | last post by:
Here is an example of a home grown generic class, representing a pair of values. Adapted from Jon Skeet's book "C# In Depth". The generic class is "sealed" for some reason (I think for performance) so you cannot derive from it, but that's optional. The main thing in this example is to show the right format for using these generic classes, which was not present in the code snippet in Skeet's book. Does anybody know why the .Equals...
0
8355
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
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
8781
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...
1
8550
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7381
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
6191
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
4193
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
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.