Why type parameter cannot used to call static method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrus

    #1

    Why type parameter cannot used to call static method

    I tried to use type parameter to call static method but got compile error
    'T' is a 'type parameter', which is not valid in the given context.

    Why this is not allowed ?
    Should I really use a lot longer ModelGenericBas e<T>.test1() ?

    Andrus.



    class VirtualGrid<T>
    where T : ModelGenericBas e<T{

    void test() {
    //'T' is a 'type parameter', which is not valid in the given context
    T.test1();
    }
    }

    class ModelGenericBas e<T{
    public static void test1() {
    }
    }


  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: Why type parameter cannot used to call static method

    Andrus wrote:
    I tried to use type parameter to call static method but got compile error
    'T' is a 'type parameter', which is not valid in the given context.
    >
    Why this is not allowed ?
    Should I really use a lot longer ModelGenericBas e<T>.test1() ?
    >
    Andrus.
    >
    >
    >
    class VirtualGrid<T>
    where T : ModelGenericBas e<T{
    >
    void test() {
    //'T' is a 'type parameter', which is not valid in the given context
    T.test1();
    }
    }
    >
    class ModelGenericBas e<T{
    public static void test1() {
    }
    }
    >
    You can't use the type parameter to call a static method, as there is no
    way of specifying in the class header that the type has to have that method.

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Christof Nordiek

      #3
      Re: Why type parameter cannot used to call static method

      Hi Andrus,

      "Andrus" <kobruleht2@hot .eeschrieb im Newsbeitrag
      news:ur$qDtSCIH A.464@TK2MSFTNG P02.phx.gbl...
      >I tried to use type parameter to call static method but got compile error
      'T' is a 'type parameter', which is not valid in the given context.
      >
      Why this is not allowed ?
      Should I really use a lot longer ModelGenericBas e<T>.test1() ?
      Yes, indeed. Why don't you want to do that?
      Only readability?
      C# doesn't support polymorphism of static members, so there would not be a
      semantical difference.

      For readability you could use a using alias directive.

      Christof

      <snip>
      >
      class VirtualGrid<T>
      where T : ModelGenericBas e<T{
      >
      void test() {
      //'T' is a 'type parameter', which is not valid in the given context
      T.test1();
      }
      }
      >
      class ModelGenericBas e<T{
      public static void test1() {
      }
      }

      Comment

      • Andrus

        #4
        Re: Why type parameter cannot used to call static method


        "Göran Andersson" <guffa@guffa.co mkirjutas sõnumis
        news:%23a4z$STC IHA.1208@TK2MSF TNGP05.phx.gbl. ..
        Andrus wrote:
        >I tried to use type parameter to call static method but got compile error
        >'T' is a 'type parameter', which is not valid in the given context.
        >>
        >Why this is not allowed ?
        >Should I really use a lot longer ModelGenericBas e<T>.test1() ?
        >>
        >Andrus.
        >>
        >>
        >>
        >class VirtualGrid<T>
        > where T : ModelGenericBas e<T{
        >>
        > void test() {
        > //'T' is a 'type parameter', which is not valid in the given context
        > T.test1();
        > }
        >}
        >>
        >class ModelGenericBas e<T{
        > public static void test1() {
        > }
        >}
        >
        You can't use the type parameter to call a static method, as there is no
        way of specifying in the class header that the type has to have that
        method.
        I specified in class header that this type has this method:

        where T : ModelGenericBas e<T>

        Is this bug ?

        Andrus.


        Comment

        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

          #5
          Re: Why type parameter cannot used to call static method

          Andrus wrote:
          >You can't use the type parameter to call a static method, as there is no
          >way of specifying in the class header that the type has to have that
          >method.
          >
          I specified in class header that this type has this method:
          >
          where T : ModelGenericBas e<T>
          >
          Is this bug ?
          >
          Andrus.
          >
          You specified that the type has to be of the type ModelGenericBas e<Tor
          inherit the type ModelGenericBas e<T>. As static methods are specific to
          the type and are not inherited, the type doesn't have to have the static
          method if it only inherits ModelGenericBas e<T>.

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          Working...