Implying method return type
-
Did you perhaps for get to escape a < in your code?
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
-
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
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
-
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
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
-
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
-
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
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.
-
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
"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 -
I have a class
Categroy
that derives from it's baseRESTResource
.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 fromRESTResource
, how Can I set the return type ofRESTResource.Get()
?/\ |_ E X E GG
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.
-
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
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
-
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.
-
No problem. I hope it helped.
Deja View - the feeling that you've seen this post before.