Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

Generics questions in .NET 2.0

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave Booker - 15 Dec 2005 17:02 GMT
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?
cody - 15 Dec 2005 17:06 GMT
instead of
where Tobject : Object

use
where Tobject : Nullable<>

haven't tried but an idea..

> Make a template of a template:
>
[quoted text clipped - 19 lines]
> must
> implement the new operator with string argument?
Nicholas Paldino [.NET/C# MVP] - 15 Dec 2005 17:13 GMT
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.

Signature

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

> Make a template of a template:
>
[quoted text clipped - 19 lines]
> must
> implement the new operator with string argument?
Dave Booker - 15 Dec 2005 19:55 GMT
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?

> Dave,
>
[quoted text clipped - 52 lines]
> > must
> > implement the new operator with string argument?
Nicholas Paldino [.NET/C# MVP] - 15 Dec 2005 20:46 GMT
   Yep.

Signature

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

> Thank you, that was very helpful.
>
[quoted text clipped - 65 lines]
>> > must
>> > implement the new operator with string argument?
Nicholas Paldino [.NET/C# MVP] - 15 Dec 2005 20:47 GMT
   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.

Signature

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

> Thank you, that was very helpful.
>
[quoted text clipped - 65 lines]
>> > must
>> > implement the new operator with string argument?
Andreas Mueller - 17 Dec 2005 13:47 GMT
> Dave,
>
[quoted text clipped - 19 lines]
>     }
> }
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
[quoted text clipped - 4 lines]
>
>     Hope this helps.
Marc Gravell - 15 Dec 2005 17:13 GMT
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

> Make a template of a template:
>
[quoted text clipped - 19 lines]
> must
> implement the new operator with string argument?
Stefan Simek - 15 Dec 2005 17:37 GMT
Hi,

See inline

> Make a template of a template:
>
[quoted text clipped - 16 lines]
>
> 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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.