Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Damian Green 452 posts 1433 karma points
    Nov 07, 2012 @ 19:10
    Damian Green
    0

    Inheriting WebUserControlBase<T>

    Ive not done any hardcore generics development so not sure how to tackle this.

    I have some common functionality that i woud like available to all my usercontrols but as I inherit from the abstract class WebUserControlBase<T> with my user control and its docType im not sure how. Maybe im missing something or going about it the wrong way?

    Ive tried creating a generic base class that would simply sit between them like this:

    public abstract class mybase<T> : Vega.USiteBuilder.WebUserControlBase<T>

    and then my usercontrol woudl inherit from my class - but the compiler dont like that.

    Error 1 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'Vega.USiteBuilder.WebUserControlBase<T>'

    Error 2 The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Vega.USiteBuilder.WebUserControlBase<T>'. There is no boxing conversion or type parameter conversion from 'T' to 'Vega.USiteBuilder.DocumentTypeBase'.

    Any pointers anyone?

     

  • Vladan Ostojic 94 posts 210 karma points
    Nov 08, 2012 @ 09:21
    Vladan Ostojic
    1

    Try this:

     

    public abstract class mybase<T> : Vega.USiteBuilder.WebUserControlBase<T>

    where T: Vega.USiteBuilder.DocumentTypeBase

    {

    }

  • Damian Green 452 posts 1433 karma points
    Nov 08, 2012 @ 10:23
    Damian Green
    0

    Still get the following error when i try that:

    Error1'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'Vega.USiteBuilder.WebUserControlBase<T>'

     

  • Damian Green 452 posts 1433 karma points
    Nov 08, 2012 @ 10:26
    Damian Green
    0

    I tried something id seen on another similar implementation and it now compiles! I'll see what happens now.

    I just added: , new() on the end

    public abstract class myBase: Vega.USiteBuilder.WebUserControlBasewhere T : Vega.USiteBuilder.DocumentTypeBase, new()

    Will post back here if it works.

  • Damian Green 452 posts 1433 karma points
    Nov 08, 2012 @ 10:48
    Damian Green
    0

    Lovely! Works perfectly. Thanks Vladan.

    If you could offer a very simple explanation to how that inheritance works id appreciate it. Id like to know how that has wired things up.

     

Please Sign in or register to post replies

Write your reply to:

Draft