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

Generics questions in .NET 2.0

Make a template of a template:

public class A<T>
{
A(string s){}
}
public class D<Tobject, Tclass> :
where Tobject : Object
where Tclass : A<Tclass>
{
public Tobject someFunction()
{
Tclass c = new Tclass("Test"); // Compiler error: Tclass doesn't have new
constraint
return null; // Compiler error: Tobject is not nullable
}
}

How do I let the compiler know that Tobject is nullable?
How do I let it know that since Tclass is constrained to types of A it must
implement the new operator with string argument?

Dec 15 '05 #1
8 1329
instead of
where Tobject : Object

use
where Tobject : Nullable<>

haven't tried but an idea..

"Dave Booker" <db******@newsgroup.nospam> schrieb im Newsbeitrag
news:2E**********************************@microsof t.com...
Make a template of a template:

public class A<T>
{
A(string s){}
}
public class D<Tobject, Tclass> :
where Tobject : Object
where Tclass : A<Tclass>
{
public Tobject someFunction()
{
Tclass c = new Tclass("Test"); // Compiler error: Tclass doesn't have new
constraint
return null; // Compiler error: Tobject is not nullable
}
}

How do I let the compiler know that Tobject is nullable?
How do I let it know that since Tclass is constrained to types of A it
must
implement the new operator with string argument?

Dec 15 '05 #2
Dave,

You won't be able to do this the way you want. There are a number of
things that need to be changed here.

First, your declarations should look like this:
public class A<T>
{
public void Initialize(string s){}
}

public class D<TObject, TClass>
where TObject : class
where TClass : A<TClass>, new()
{
public TObject someFunction()
{
TClass c = new TClass();
c.Initialize("Test");
return null;
}
}

When you use "class" as a constraint, it means that you can only pass in
reference types. This is what allows null to be returned for TObject. The
second thing is you have to declare your initialization outsize of the
constructor. The reason for this is that the new constraint only constrains
on parameterless constructors. Because of this, you need a separate method
to call on A which can perform the necessary setup.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dave Booker" <db******@newsgroup.nospam> wrote in message
news:2E**********************************@microsof t.com...
Make a template of a template:

public class A<T>
{
A(string s){}
}
public class D<Tobject, Tclass> :
where Tobject : Object
where Tclass : A<Tclass>
{
public Tobject someFunction()
{
Tclass c = new Tclass("Test"); // Compiler error: Tclass doesn't have new
constraint
return null; // Compiler error: Tobject is not nullable
}
}

How do I let the compiler know that Tobject is nullable?
How do I let it know that since Tclass is constrained to types of A it
must
implement the new operator with string argument?

Dec 15 '05 #3

Re the new(string) signature, you can't do this at design time (yet - maybe
this will get more powerful in 3.0?). One option is to use reflection to
test this in a static ctor (I posted an example of this a week-or-so ago) -
you'd also need to use the reflected ctor to create your object, so you
could perhaps persist this in a private static field; the validation will
only happen at runtime, but at least it will happen as early as possible.

The Tobject : Object doesn't look like it will do much, since everything is
derived from Object

When you say nullable, do you need to be able to handle Nullable<T>? or just
classes (reference types). If the latter, you could replace this line with
Tobject : class; this then compiles.

Hope this helps,

Marc

"Dave Booker" <db******@newsgroup.nospam> wrote in message
news:2E**********************************@microsof t.com...
Make a template of a template:

public class A<T>
{
A(string s){}
}
public class D<Tobject, Tclass> :
where Tobject : Object
where Tclass : A<Tclass>
{
public Tobject someFunction()
{
Tclass c = new Tclass("Test"); // Compiler error: Tclass doesn't have new
constraint
return null; // Compiler error: Tobject is not nullable
}
}

How do I let the compiler know that Tobject is nullable?
How do I let it know that since Tclass is constrained to types of A it
must
implement the new operator with string argument?

Dec 15 '05 #4
Hi,

See inline

Dave Booker wrote:
Make a template of a template:

public class A<T>
{
A(string s){}
}
public class D<Tobject, Tclass> :
where Tobject : Object
where Tclass : A<Tclass>
{
public Tobject someFunction()
{
Tclass c = new Tclass("Test"); // Compiler error: Tclass doesn't have new
constraint
return null; // Compiler error: Tobject is not nullable
}
}

How do I let the compiler know that Tobject is nullable? The Tobject : object constraint doesn't do anything at all, because
everything in the .NET world derives (or can be boxed in order to
derive) from an object. The right constraint to specify a reference type
would beTobjkec
How do I let it know that since Tclass is constrained to types of A it must
implement the new operator with string argument?

Dec 15 '05 #5
Thank you, that was very helpful.

So just to confirm: in C# 2.0 is there no way to define a generic class so
that the template type can be instantiated inside the class, unless the
template type is explicitly constrained to "new()" AND the template type must
define a parameterless constructor?

"Nicholas Paldino [.NET/C# MVP]" wrote:
Dave,

You won't be able to do this the way you want. There are a number of
things that need to be changed here.

First, your declarations should look like this:
public class A<T>
{
public void Initialize(string s){}
}

public class D<TObject, TClass>
where TObject : class
where TClass : A<TClass>, new()
{
public TObject someFunction()
{
TClass c = new TClass();
c.Initialize("Test");
return null;
}
}

When you use "class" as a constraint, it means that you can only pass in
reference types. This is what allows null to be returned for TObject. The
second thing is you have to declare your initialization outsize of the
constructor. The reason for this is that the new constraint only constrains
on parameterless constructors. Because of this, you need a separate method
to call on A which can perform the necessary setup.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dave Booker" <db******@newsgroup.nospam> wrote in message
news:2E**********************************@microsof t.com...
Make a template of a template:

public class A<T>
{
A(string s){}
}
public class D<Tobject, Tclass> :
where Tobject : Object
where Tclass : A<Tclass>
{
public Tobject someFunction()
{
Tclass c = new Tclass("Test"); // Compiler error: Tclass doesn't have new
constraint
return null; // Compiler error: Tobject is not nullable
}
}

How do I let the compiler know that Tobject is nullable?
How do I let it know that since Tclass is constrained to types of A it
must
implement the new operator with string argument?


Dec 15 '05 #6
Yep.

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

"Dave Booker" <db******@newsgroup.nospam> wrote in message
news:F8**********************************@microsof t.com...
Thank you, that was very helpful.

So just to confirm: in C# 2.0 is there no way to define a generic class so
that the template type can be instantiated inside the class, unless the
template type is explicitly constrained to "new()" AND the template type
must
define a parameterless constructor?

"Nicholas Paldino [.NET/C# MVP]" wrote:
Dave,

You won't be able to do this the way you want. There are a number of
things that need to be changed here.

First, your declarations should look like this:
public class A<T>
{
public void Initialize(string s){}
}

public class D<TObject, TClass>
where TObject : class
where TClass : A<TClass>, new()
{
public TObject someFunction()
{
TClass c = new TClass();
c.Initialize("Test");
return null;
}
}

When you use "class" as a constraint, it means that you can only pass
in
reference types. This is what allows null to be returned for TObject.
The
second thing is you have to declare your initialization outsize of the
constructor. The reason for this is that the new constraint only
constrains
on parameterless constructors. Because of this, you need a separate
method
to call on A which can perform the necessary setup.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dave Booker" <db******@newsgroup.nospam> wrote in message
news:2E**********************************@microsof t.com...
> Make a template of a template:
>
> public class A<T>
> {
> A(string s){}
> }
>
>
> public class D<Tobject, Tclass> :
> where Tobject : Object
> where Tclass : A<Tclass>
> {
> public Tobject someFunction()
> {
> Tclass c = new Tclass("Test"); // Compiler error: Tclass doesn't have
> new
> constraint
> return null; // Compiler error: Tobject is not nullable
> }
> }
>
> How do I let the compiler know that Tobject is nullable?
> How do I let it know that since Tclass is constrained to types of A it
> must
> implement the new operator with string argument?
>


Dec 15 '05 #7
Well, I spoke too soon. You could get the type of T, and then get the
constructor and call that through reflection. This assumes you know what
the parameter list of the constructor will be, however.

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

"Dave Booker" <db******@newsgroup.nospam> wrote in message
news:F8**********************************@microsof t.com...
Thank you, that was very helpful.

So just to confirm: in C# 2.0 is there no way to define a generic class so
that the template type can be instantiated inside the class, unless the
template type is explicitly constrained to "new()" AND the template type
must
define a parameterless constructor?

"Nicholas Paldino [.NET/C# MVP]" wrote:
Dave,

You won't be able to do this the way you want. There are a number of
things that need to be changed here.

First, your declarations should look like this:
public class A<T>
{
public void Initialize(string s){}
}

public class D<TObject, TClass>
where TObject : class
where TClass : A<TClass>, new()
{
public TObject someFunction()
{
TClass c = new TClass();
c.Initialize("Test");
return null;
}
}

When you use "class" as a constraint, it means that you can only pass
in
reference types. This is what allows null to be returned for TObject.
The
second thing is you have to declare your initialization outsize of the
constructor. The reason for this is that the new constraint only
constrains
on parameterless constructors. Because of this, you need a separate
method
to call on A which can perform the necessary setup.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dave Booker" <db******@newsgroup.nospam> wrote in message
news:2E**********************************@microsof t.com...
> Make a template of a template:
>
> public class A<T>
> {
> A(string s){}
> }
>
>
> public class D<Tobject, Tclass> :
> where Tobject : Object
> where Tclass : A<Tclass>
> {
> public Tobject someFunction()
> {
> Tclass c = new Tclass("Test"); // Compiler error: Tclass doesn't have
> new
> constraint
> return null; // Compiler error: Tobject is not nullable
> }
> }
>
> How do I let the compiler know that Tobject is nullable?
> How do I let it know that since Tclass is constrained to types of A it
> must
> implement the new operator with string argument?
>


Dec 15 '05 #8
Nicholas Paldino [.NET/C# MVP] wrote:
Dave,

You won't be able to do this the way you want. There are a number of
things that need to be changed here.

First, your declarations should look like this:
public class A<T>
{
public void Initialize(string s){}
}

public class D<TObject, TClass>
where TObject : class
where TClass : A<TClass>, new()
{
public TObject someFunction()
{
TClass c = new TClass();
c.Initialize("Test");
return null;
}
} just to round it up, Tobject can be a value type, too:
public class D<TObject, TClass>
where TClass : A<TClass>, new()
{
public TObject someFunction()
{
TClass c = new TClass();
c.Initialize("Test");
return default(TObject);
}
}
When you use "class" as a constraint, it means that you can only pass in
reference types. This is what allows null to be returned for TObject. The
second thing is you have to declare your initialization outsize of the
constructor. The reason for this is that the new constraint only constrains
on parameterless constructors. Because of this, you need a separate method
to call on A which can perform the necessary setup.

Hope this helps.

Dec 17 '05 #9

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

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
18
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
3
by: LongBow | last post by:
Hello all, First of all, sorry for multiple question per one thread. I have two questions. First what I think might be the easier problem. I am capturing data from an embedded device which I...
0
by: ashok2006 | last post by:
Hi every body. Can some body suggest any website where I can find practice questions for java generics and collection (new to Java 1.5). Thanx.
10
by: Frank Rizzo | last post by:
Given the inneficiencies of ArrayList and Hashtable on 64-bit systems, I am converting them to List<and Dictionary<respectively. It's a pretty massive system, so there are a lot of casts. For...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
10
by: Udi | last post by:
Hi All, Two questions regarding Generics: 1. I'd like to constrain my Type parameter to be one of a preefined group of types. For example: class Field<Twhere T: Byte, Int, String,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.