473,221 Members | 2,003 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,221 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 1327
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,...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
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...
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.