471,354 Members | 1,190 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 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 1287
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Luc Vaillant | last post: by
9 posts views Thread by sloan | last post: by
18 posts views Thread by riftimes | last post: by
3 posts views Thread by LongBow | last post: by
10 posts views Thread by Frank Rizzo | last post: by
13 posts views Thread by rkausch | last post: by
10 posts views Thread by Udi | last post: by

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.