Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Implying method return type

Implying method return type

Scheduled Pinned Locked Moved C#
questiontutorial
18 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eggie5
    wrote on last edited by
    #1

    I have a class Categroy that derives from it's base RESTResource.

    public class Category : RESTResource
    {
    }

    public class RESTResource
    {
    public < derived type > Get()
    {
    }
    }

    The above example should evaluate to ... public Category Get() ... at runtime. Depending on the Type of the derviced class from RESTResource, how Can I set the return type of RESTResource.Get()?

    /\ |_ E X E GG

    C P 2 Replies Last reply
    0
    • E eggie5

      I have a class Categroy that derives from it's base RESTResource.

      public class Category : RESTResource
      {
      }

      public class RESTResource
      {
      public < derived type > Get()
      {
      }
      }

      The above example should evaluate to ... public Category Get() ... at runtime. Depending on the Type of the derviced class from RESTResource, how Can I set the return type of RESTResource.Get()?

      /\ |_ E X E GG

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I'm not sure if you can write generic properties. If not, all you can do is return the base type, and users would have to cast it. I don't think you can override a property and change it's return type.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      E 1 Reply Last reply
      0
      • C Christian Graus

        I'm not sure if you can write generic properties. If not, all you can do is return the base type, and users would have to cast it. I don't think you can override a property and change it's return type.

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        E Offline
        E Offline
        eggie5
        wrote on last edited by
        #3

        I guess ruby got my hopes up...

        /\ |_ E X E GG

        T 1 Reply Last reply
        0
        • E eggie5

          I guess ruby got my hopes up...

          /\ |_ E X E GG

          T Offline
          T Offline
          TJoe
          wrote on last edited by
          #4

          It really shouldn't matter that you can't change the return type. Assuming that you could, you would still need to declare a variable to hold it using the dervied type. This implies that you know the type like:

          RESTResource r = /* set from somewhere else, but is actually a Category */;

          //...

          // Assume Get()'s return type is Category
          Category c = r.Get();

          Since you need to know the return type anyway, you can simply set the return type of Get to RESTResource then cast it like so:

          RESTResource r = /* set from somewhere else, but is actually a Category */;

          //...

          // Assume Get()'s return type is RESTResource
          Category c = r.Get() as Category;
          if (null != c)
          // c is a Category

          Now, you may be referring to virtual functions, which work like this:

          public class RESTResource {
          public virtual RESTResource Get() {
          return new RESTResource();
          }
          }

          public class Category : RESTResource {
          public override RESTResource Get() {
          return new Category();
          }
          }

          // ... some other method ...
          RESTResource r = new RESTResource();
          RESTResource r2 = r.Get(); // returns a RESTResource

          Category c = new Category();
          Category c2 = c.Get() as Category; // returns a RESTResource, which is actually an instance of Category so we need to cast it.

          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

          E 1 Reply Last reply
          0
          • T TJoe

            It really shouldn't matter that you can't change the return type. Assuming that you could, you would still need to declare a variable to hold it using the dervied type. This implies that you know the type like:

            RESTResource r = /* set from somewhere else, but is actually a Category */;

            //...

            // Assume Get()'s return type is Category
            Category c = r.Get();

            Since you need to know the return type anyway, you can simply set the return type of Get to RESTResource then cast it like so:

            RESTResource r = /* set from somewhere else, but is actually a Category */;

            //...

            // Assume Get()'s return type is RESTResource
            Category c = r.Get() as Category;
            if (null != c)
            // c is a Category

            Now, you may be referring to virtual functions, which work like this:

            public class RESTResource {
            public virtual RESTResource Get() {
            return new RESTResource();
            }
            }

            public class Category : RESTResource {
            public override RESTResource Get() {
            return new Category();
            }
            }

            // ... some other method ...
            RESTResource r = new RESTResource();
            RESTResource r2 = r.Get(); // returns a RESTResource

            Category c = new Category();
            Category c2 = c.Get() as Category; // returns a RESTResource, which is actually an instance of Category so we need to cast it.

            Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

            E Offline
            E Offline
            eggie5
            wrote on last edited by
            #5

            Thanks. I think I'm going to head in a different direction with this... Now, is this the right use of generics? Category has a static string endpoint_url. I would like to get that from T1 in the Create method.

            Create<Category>();
            .
            .
            .

            public Response Create<T1>()
            {
                string url=//How can I get Category.endpoint\_url here?
            }
            
            public class Category
            {
                public static string endpoint\_url = "/categories";
                public int id;
                public string name;
                public int content\_provider\_id;
            
            } 
            

            -- modified at 1:50 Tuesday 2nd October, 2007

            /\ |_ E X E GG

            T 2 Replies Last reply
            0
            • E eggie5

              Thanks. I think I'm going to head in a different direction with this... Now, is this the right use of generics? Category has a static string endpoint_url. I would like to get that from T1 in the Create method.

              Create<Category>();
              .
              .
              .

              public Response Create<T1>()
              {
                  string url=//How can I get Category.endpoint\_url here?
              }
              
              public class Category
              {
                  public static string endpoint\_url = "/categories";
                  public int id;
                  public string name;
                  public int content\_provider\_id;
              
              } 
              

              -- modified at 1:50 Tuesday 2nd October, 2007

              /\ |_ E X E GG

              T Offline
              T Offline
              TJoe
              wrote on last edited by
              #6

              Your code isn't complete, so I can't really tell what you are doing (or want to do). Can you provide a little more info or more complete example?

              Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

              E 1 Reply Last reply
              0
              • E eggie5

                Thanks. I think I'm going to head in a different direction with this... Now, is this the right use of generics? Category has a static string endpoint_url. I would like to get that from T1 in the Create method.

                Create<Category>();
                .
                .
                .

                public Response Create<T1>()
                {
                    string url=//How can I get Category.endpoint\_url here?
                }
                
                public class Category
                {
                    public static string endpoint\_url = "/categories";
                    public int id;
                    public string name;
                    public int content\_provider\_id;
                
                } 
                

                -- modified at 1:50 Tuesday 2nd October, 2007

                /\ |_ E X E GG

                T Offline
                T Offline
                TJoe
                wrote on last edited by
                #7

                Did you perhaps for get to escape a < in your code?

                Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                E 1 Reply Last reply
                0
                • T TJoe

                  Your code isn't complete, so I can't really tell what you are doing (or want to do). Can you provide a little more info or more complete example?

                  Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                  E Offline
                  E Offline
                  eggie5
                  wrote on last edited by
                  #8

                  I have this class structure:

                  public class Test
                  {
                    public Response Create<T1>()
                    {
                      string url=//How can I get Category.endpoint\_url here?
                    }
                  }
                  
                  public class Category
                  {
                      public static string endpoint\_url = "/categories";
                      public int id;
                      public string name;
                      public int content\_provider\_id;
                  
                  } 
                  

                  My Category class has a static string endpoint_url. I want to get that string from inside of create using the Type T1 and save it to url. My expected results are to have url set to "/categories"

                  /\ |_ E X E GG

                  T 1 Reply Last reply
                  0
                  • T TJoe

                    Did you perhaps for get to escape a < in your code?

                    Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                    E Offline
                    E Offline
                    eggie5
                    wrote on last edited by
                    #9

                    just fixed it.

                    /\ |_ E X E GG

                    1 Reply Last reply
                    0
                    • E eggie5

                      I have this class structure:

                      public class Test
                      {
                        public Response Create<T1>()
                        {
                          string url=//How can I get Category.endpoint\_url here?
                        }
                      }
                      
                      public class Category
                      {
                          public static string endpoint\_url = "/categories";
                          public int id;
                          public string name;
                          public int content\_provider\_id;
                      
                      } 
                      

                      My Category class has a static string endpoint_url. I want to get that string from inside of create using the Type T1 and save it to url. My expected results are to have url set to "/categories"

                      /\ |_ E X E GG

                      T Offline
                      T Offline
                      TJoe
                      wrote on last edited by
                      #10

                      You can either create an abstract base class or an interface (from which Category and others would derive). If Category and any possible other classes have common functionality, then you should go with an abstract base class, because you can put that common functionality in there. If you go with an interface, then your classes (e.g. Category) really won't share code. You won't be able to get the static endpoint_url without creating an instance of Category. Both methods above require an instance of Category. Assuming you are willing to create an instance of Category, the code would be like this:

                      public Response Create<T>() where T : MyBaseClass {
                      T myClass = new T();
                      String url = myClass.GetURL();
                      // ....
                      }

                      // ...

                      public abstract class MyBaseClass {
                      public abstract String GetURL();

                      // Other method implementations can go here
                      

                      }

                      public class Category : MyBaseClass {
                      public static string endpoint_url = "/categories";

                      public override String GetURL() {
                          return Category.endpoint\_url;
                      }
                      

                      }

                      // Then you can call Create like this, where t is of type Test
                      Response r = t.Create();

                      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                      E 2 Replies Last reply
                      0
                      • T TJoe

                        You can either create an abstract base class or an interface (from which Category and others would derive). If Category and any possible other classes have common functionality, then you should go with an abstract base class, because you can put that common functionality in there. If you go with an interface, then your classes (e.g. Category) really won't share code. You won't be able to get the static endpoint_url without creating an instance of Category. Both methods above require an instance of Category. Assuming you are willing to create an instance of Category, the code would be like this:

                        public Response Create<T>() where T : MyBaseClass {
                        T myClass = new T();
                        String url = myClass.GetURL();
                        // ....
                        }

                        // ...

                        public abstract class MyBaseClass {
                        public abstract String GetURL();

                        // Other method implementations can go here
                        

                        }

                        public class Category : MyBaseClass {
                        public static string endpoint_url = "/categories";

                        public override String GetURL() {
                            return Category.endpoint\_url;
                        }
                        

                        }

                        // Then you can call Create like this, where t is of type Test
                        Response r = t.Create();

                        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                        E Offline
                        E Offline
                        eggie5
                        wrote on last edited by
                        #11

                        TJoe wrote:

                        If Category and any possible other classes have common functionality, then you should go with an abstract base class

                        Yeah, I have a few other classes... I was just using Category as an example. I'll try out what you've worked out first thing tomorrow. Thanks, Alex

                        /\ |_ E X E GG

                        1 Reply Last reply
                        0
                        • T TJoe

                          You can either create an abstract base class or an interface (from which Category and others would derive). If Category and any possible other classes have common functionality, then you should go with an abstract base class, because you can put that common functionality in there. If you go with an interface, then your classes (e.g. Category) really won't share code. You won't be able to get the static endpoint_url without creating an instance of Category. Both methods above require an instance of Category. Assuming you are willing to create an instance of Category, the code would be like this:

                          public Response Create<T>() where T : MyBaseClass {
                          T myClass = new T();
                          String url = myClass.GetURL();
                          // ....
                          }

                          // ...

                          public abstract class MyBaseClass {
                          public abstract String GetURL();

                          // Other method implementations can go here
                          

                          }

                          public class Category : MyBaseClass {
                          public static string endpoint_url = "/categories";

                          public override String GetURL() {
                              return Category.endpoint\_url;
                          }
                          

                          }

                          // Then you can call Create like this, where t is of type Test
                          Response r = t.Create();

                          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                          E Offline
                          E Offline
                          eggie5
                          wrote on last edited by
                          #12

                          I get a compile error on this line: T myClass = new T(); Error 1 Cannot create an instance of the variable type 'T' because it does not have the new() constraint

                          /\ |_ E X E GG

                          P D T 3 Replies Last reply
                          0
                          • E eggie5

                            I get a compile error on this line: T myClass = new T(); Error 1 Cannot create an instance of the variable type 'T' because it does not have the new() constraint

                            /\ |_ E X E GG

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #13

                            This means that the constraint for the generic is missing the new() keyword. For example:

                            public T Create() where T : ISomething, new()
                            {
                              T item = new T();
                              return item;
                            }
                            

                            The new constraint can only be used with a zero-arg constructor.

                            Deja View - the feeling that you've seen this post before.

                            1 Reply Last reply
                            0
                            • E eggie5

                              I get a compile error on this line: T myClass = new T(); Error 1 Cannot create an instance of the variable type 'T' because it does not have the new() constraint

                              /\ |_ E X E GG

                              D Offline
                              D Offline
                              DavidNohejl
                              wrote on last edited by
                              #14

                              "Because not all objects are guaranteed to have public default constructors, the compiler does not allow you to call the default constructor on the type parameter. To override this compiler restriction, you add the text new() after all other constraints are specified. This text is a constructor constraint, and it forces the type parameter decorated with the constructor constraint to have a default constructor." (http://www.codeproject.com/books/EssentialCS20.asp[^]) Fix is easy, public Response Create() where T : MyBaseClass, New()


                              [My Blog]
                              "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - RĂ¼diger Klaehn
                              "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                              1 Reply Last reply
                              0
                              • E eggie5

                                I have a class Categroy that derives from it's base RESTResource.

                                public class Category : RESTResource
                                {
                                }

                                public class RESTResource
                                {
                                public < derived type > Get()
                                {
                                }
                                }

                                The above example should evaluate to ... public Category Get() ... at runtime. Depending on the Type of the derviced class from RESTResource, how Can I set the return type of RESTResource.Get()?

                                /\ |_ E X E GG

                                P Offline
                                P Offline
                                Pete OHanlon
                                wrote on last edited by
                                #15

                                This will do it.

                                public class Category : RESTResource<Category>
                                {
                                }
                                
                                public class RESTResource<T> where T : RESTResource, new()
                                {
                                  public <T> Get()
                                  {
                                    T item = new T();
                                    // do some work;
                                    return item;
                                  }
                                }
                                

                                Deja View - the feeling that you've seen this post before.

                                E 1 Reply Last reply
                                0
                                • E eggie5

                                  I get a compile error on this line: T myClass = new T(); Error 1 Cannot create an instance of the variable type 'T' because it does not have the new() constraint

                                  /\ |_ E X E GG

                                  T Offline
                                  T Offline
                                  TJoe
                                  wrote on last edited by
                                  #16

                                  Sorry, forgot about the new() keyword. Pete's response describes how to use it.

                                  Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                                  1 Reply Last reply
                                  0
                                  • P Pete OHanlon

                                    This will do it.

                                    public class Category : RESTResource<Category>
                                    {
                                    }
                                    
                                    public class RESTResource<T> where T : RESTResource, new()
                                    {
                                      public <T> Get()
                                      {
                                        T item = new T();
                                        // do some work;
                                        return item;
                                      }
                                    }
                                    

                                    Deja View - the feeling that you've seen this post before.

                                    E Offline
                                    E Offline
                                    eggie5
                                    wrote on last edited by
                                    #17

                                    Thanks

                                    /\ |_ E X E GG

                                    P 1 Reply Last reply
                                    0
                                    • E eggie5

                                      Thanks

                                      /\ |_ E X E GG

                                      P Offline
                                      P Offline
                                      Pete OHanlon
                                      wrote on last edited by
                                      #18

                                      No problem. I hope it helped.

                                      Deja View - the feeling that you've seen this post before.

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups