473,507 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics: How can I create 'optional' Type parameters

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()

The class has many 'use case' so it can be happen that TA or TB or TC (or TA
and TB or etc..)is meaningless. (similar to null actual parameters) There
are to many combinations

How can I instantiate a closed type instance from this generic in this cases
and how can I detect in UniversalClass's code this cases?

What I've tried:

I've created a NullTypeParamerer class for this purpose, and I test for it
in UniversalClass's constructor:
if( typeof(NullTypeParameter).IsAssignableFrom(typeof( TA)) ) {...}

My problem that in case I must use 'typeless' where clause like this:

class UniversalClass<TA, TB, TC>()
where TA: NullTypeParameter, new()
where TB: NullTypeParameter, new()
where TC: NullTypeParameter, new()

and all of A, B, C actual types must be inherited from NullTypeParameter. I
am not happy with this...

Any ideas?



Aug 23 '06 #1
2 15617
john wrote:
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()

The class has many 'use case' so it can be happen that TA or TB or TC (or TA
and TB or etc..)is meaningless. (similar to null actual parameters) There
are to many combinations

How can I instantiate a closed type instance from this generic in this cases
and how can I detect in UniversalClass's code this cases?
You can overload the generic type name 'UniversalClass' based on the
number of generic parameters. In other words, use overloading to
simulate default argument values in the same way as one does for normal
methods.

To fill in your default value, you could have each type with fewer
parameters derive from the type with one more parameter, and pass in the
default type as an argument to the ancestor.

If you can't choose a meaningful default type, then you'll need separate
class definitions for each instance, but you can still get the
overloading based on generic parameter count.

-- Barry

--
http://barrkel.blogspot.com/
Aug 23 '06 #2
"john" <jo**@nospam.coma écrit dans le message de news:
O9*************@TK2MSFTNGP06.phx.gbl...

| 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()
|
| The class has many 'use case' so it can be happen that TA or TB or TC (or
TA
| and TB or etc..)is meaningless. (similar to null actual parameters) There
| are to many combinations

Optional type parameters just do not make sense. Generic classes are fixed,
typesafe classes that are meant to be totally predictable in their
behaviour. Are you sure that you are not trying to achieve some other goal ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Aug 23 '06 #3

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

Similar topics

1
1426
by: Julia | last post by:
Hi, I wonder can I dynamically create a type without knowing the exact assembly in which he reside but with knowing that his assembly is referenced in the project? surely I can list(at rn...
2
4001
by: david.kao | last post by:
I have web service always return a string in Xml format, and I have the code at client side translates this return string into correct value. For example: my web service returns a <ret...
3
2220
by: Levi | last post by:
class A<Ta> { public A() { } } class B<TA, Ta> where TA : A<Ta> { public B() { } }
2
1689
by: ABC | last post by:
Can I create an Interface which have a method have variate type parameters? I want create an Interface named IDataEditable which have two methods: LoadRecord and SaveRecord. As the LoadRecord...
0
1056
by: buru | last post by:
I try to create tris type: create type adr_ty as object( str varchar2(30), nr integer, bl integer, ap integer); but I get this wraning: Type created with compilation errors
4
1420
oll3i
by: oll3i | last post by:
cd u give me the example of constrained type parameters
1
2627
by: pmayer995 | last post by:
To anyone from Microsoft, it'd be very helpful to have generics be able to inherit from type parameters, i.e.: public class FieldControl<T: T where T : WebControl should compile instead of...
1
2519
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...
0
1288
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm using Visual Basic in VS2005. How many different ways I can create a type dataset and a untype dataset? Ways to create type dataset 1. Use Dataset Designer 2. Use Data Source...
0
7223
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,...
0
7321
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
7377
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...
0
7488
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...
0
5623
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,...
1
5045
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...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
412
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...

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.