|
I have this class structure:
public class Test
{
public Response Create<T1>()
{
string url=
}
}
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();
}
public class Category : MyBaseClass {
public static string endpoint_url = "/categories";
public override String GetURL() {
return Category.endpoint_url;
}
}
Response r = t.Create<Category>();
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
|
|
|
|
|
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.
|
|
|
|
|
"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<T>() 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
|
|
|
|
|
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
|
|
|
|
|
Did you perhaps for get to escape a < in your code?
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
just fixed it.
/\ |_ 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();
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.
|
|
|
|
|
I'm developing a windows application in C# with crystal reports. In one of my reports I deisplay passenger list comes by cruises. I group it by cruise no. I want to display group footer with total no of passengers by their nationality and grand total, something like this,
Malaysia 10
Singapore 15
Thailand 10
Total 35
and I need to do the same thing at the report footer as well with totals of groups. Can anybody tell me how to do it.
|
|
|
|
|
Dear All,
I am making a NMS (Network Monitoring System) and have accomplished to query windows based computers via the WMI (which by the way is very helpful).
Now my target is to embed the SNMP functionality inorder to achieve status of Managed Network devices (such as switches and routers...) to be displayed into my software.
I have seen codes at CP but there is a wrapper class that available but I am looking for is making it right from the scratch in C#
Please Advice
Thank you!
M. Nauman Yousuf
"Mess with the Best, Die like the rest"
|
|
|
|
|
ive got a very simple program set up in vs 2003 written in c#...
it includes a ref to system.diagnostics and uses process.start(@"c:/meh.bat") to try and run a batch script... the code runs without error, and i see the console window flash before me, but it seems as though nothing happened... so to test it out i setup the batch script to run notepad... i double click on the script through windows, and there's notepad... i run the program which SHOULD run the batch script, and nothing happens.... anyone have a clue as to what might be going on?
|
|
|
|
|
Didn't you want a backslash rather than a slash?
|
|
|
|
|
yes, im sure its right in the code though, i was just trying to get the point across and i dont have the code in front of me
when i made it just to be safe i went to start>run>browse, clicked the batch script then copied the exact text from the run window to be sure it was err free
|
|
|
|
|
The window closes as soon as the executable exits.
You can add a System.Console.ReadLine() to the end of your app so it stays open until you press return.
|
|
|
|
|
How do you make it so an if statement has a lot of things to match? It's hard to explain, but instead of '&&', is there a way to make it so you can have a bunch of if's to follow? I hope you understand what I mean... Thanks in advance, M.S.
|
|
|
|
|
You mean like this?
if ( a )
{
if ( b )
{
...
}
}
|
|
|
|
|
You can do one of three things:
1 - if (x == 0 && y ==1 && z == 2)
2 - if (x == 0)
{
if(y == 1)
{
if ( z== 2 )
{
3 - if (ValidateForm())
{
}
where you have a lot of stuff to check and you factor it into a function ( this works only if the things you are validating are member variables )
If you had lots of different ifs, as in
if (x == 0 )
{
}
else if (x == 1)
{
}
else if (x==2)
then you'd want a switch statement. But, if you have a lot of things to check, there's no way around that you need to specify what they all are.
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 )
|
|
|
|
|
Hi,
MasterSharp wrote: Forum C#
Subject: Re: More if's?
Sender: MasterSharp
Date: 20:08 1 Oct '07
How do you make it so an if statement has a lot of things to match? It's hard to explain, but instead of '&&', is there a way to make it so you can have a bunch of if's to follow? I hope you understand what I mean... Thanks in advance, M.S.
The long and short of it is that you can chain together as many expressions as you like with &&'s and ||'s.
I'd recommend picking up an introductory C# book - there are a number of good ones. Jesse Liberty's "Learning C#" is a good one, or you might find the Head First C# book interesting to read - it isn't your standard C# text. Apress has some good ones too. I have several books on the subject and each provides a different perspective.
I think that you'd really benefit by getting one of these books and working through it. Most have exercises, and those are helpful. There are a bunch of great folks here at Code Project who can answer questions, but you're not going to learn the language nearly as well as if you invest some time with a good text - your question indicates that you are new to the language and probably haven't worked through an introductory text, because just about any good introductory text would have provided the answer to that question and TONS of others you are likely to have along the way.
.NET development, and programming in general, is a journey and NONE of us has it all figured out - if you'll invest the time upfront with a good text on the language, and then experiment along the way, read the MS-provided help pages, you'll likely be quite capable at developing C# apps in a reasonable period of time. Of course, when you are stumped on a program, there are a bunch of folks here that would be happy to take a look at the code snippet and respond. At least that has been my experience.
Good Luck!
It isn't enough to do well in life.
One must do good when and where one can.
Otherwise, what's the point?
|
|
|
|
|
No, actually, I mean (though this isn't correct, something close to the idea), like this:
if (condition)<br />
|| condition<br />
|| condition <br />
and so on...
or is that what the || operator does?
|
|
|
|
|
Like this?
if ( a )
{
...
}
else
{
if ( b )
{
...
}
else
{
...
}
}
|
|
|
|
|
No, I mean having more things that have to be true (for example) in the if statement? I'm sorry, it's complicated...
|
|
|
|