CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2001
    Location
    Sweden
    Posts
    82

    Type parameter question

    Hi,

    I've defined a generic class where I tried to access a static method in
    'T', but I got the error message: "'T' is a 'type parameter', which is not valid in the given context"


    Code:
    public class MyGenericClass<T>
        where T :  new()
    {
          public void MyMethod()
         {
              string s = T.GetString();  // GetString() is a static method
                         ^ 
                         error here
    
         }
    }
    Is there something missing in the constraints or isn't it possible to access static methods in the type parameter ?
    PS: I'm using MVS 2005
    Last edited by NetMaster; September 19th, 2008 at 05:05 AM.

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Type parameter question

    Generics don't work this way. Moreover, it seems to me like bad design. Maybe, you want generic static method?
    Code:
    public static class StringFactory
    {
      public static string GetString<T>()
      {
        // determine return value on basis of T
      }
    }
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Feb 2001
    Location
    Sweden
    Posts
    82

    Re: Type parameter question

    Maybe it is bad design. I'll have to solve the problem in some other way.
    Thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured