Help me impliment this class structure
-
I have a class Category, that derives from the ServiceResource class.
//Base class for all resources //Lots of these properties must be overridden by the derived class //Starting to think it makes more sense for this to be an interface instead... public class ServiceResource { public string endpoint\_url; //this MUST be over ridden by the derived class //this takes the object and saves it via API call public ServiceResponse Save() { string post\_params = BuildParameters(); //this should call BuildParameters in the derived class ServiceRequest request = new ServiceRequest(); ServiceResponse resp = request.Send("POST", this.endpoint\_url, post\_params); return resp; } public string BuildParameters() { throw new Exception("You must set this in the derived class"); //this MUST overridded by descendent class so this class can make the requests to the API endpoint } }
And the Category class:
public class Category : ServiceResource
{
private int id;
private string name;public string BuildParameters() { return "I am overriding base.BuildParameters()"; } //Override base.endpoint\_url public string endpoint\_url = "/categories"; public int Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } }
Now I would like to be able to use the Category class like this:
Category c = new Category();
c.Name="new category";
c.Save();Now when Save is called, it will call the method BuildParameters. However, I want it to call BuildParameters in the derived class and not the base class, same with endpoint_url. At this point, base.BuildParameters is called instead. How can I do this.
/\ |_ E X E GG
-
I have a class Category, that derives from the ServiceResource class.
//Base class for all resources //Lots of these properties must be overridden by the derived class //Starting to think it makes more sense for this to be an interface instead... public class ServiceResource { public string endpoint\_url; //this MUST be over ridden by the derived class //this takes the object and saves it via API call public ServiceResponse Save() { string post\_params = BuildParameters(); //this should call BuildParameters in the derived class ServiceRequest request = new ServiceRequest(); ServiceResponse resp = request.Send("POST", this.endpoint\_url, post\_params); return resp; } public string BuildParameters() { throw new Exception("You must set this in the derived class"); //this MUST overridded by descendent class so this class can make the requests to the API endpoint } }
And the Category class:
public class Category : ServiceResource
{
private int id;
private string name;public string BuildParameters() { return "I am overriding base.BuildParameters()"; } //Override base.endpoint\_url public string endpoint\_url = "/categories"; public int Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } }
Now I would like to be able to use the Category class like this:
Category c = new Category();
c.Name="new category";
c.Save();Now when Save is called, it will call the method BuildParameters. However, I want it to call BuildParameters in the derived class and not the base class, same with endpoint_url. At this point, base.BuildParameters is called instead. How can I do this.
/\ |_ E X E GG
-
I have a class Category, that derives from the ServiceResource class.
//Base class for all resources //Lots of these properties must be overridden by the derived class //Starting to think it makes more sense for this to be an interface instead... public class ServiceResource { public string endpoint\_url; //this MUST be over ridden by the derived class //this takes the object and saves it via API call public ServiceResponse Save() { string post\_params = BuildParameters(); //this should call BuildParameters in the derived class ServiceRequest request = new ServiceRequest(); ServiceResponse resp = request.Send("POST", this.endpoint\_url, post\_params); return resp; } public string BuildParameters() { throw new Exception("You must set this in the derived class"); //this MUST overridded by descendent class so this class can make the requests to the API endpoint } }
And the Category class:
public class Category : ServiceResource
{
private int id;
private string name;public string BuildParameters() { return "I am overriding base.BuildParameters()"; } //Override base.endpoint\_url public string endpoint\_url = "/categories"; public int Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } }
Now I would like to be able to use the Category class like this:
Category c = new Category();
c.Name="new category";
c.Save();Now when Save is called, it will call the method BuildParameters. However, I want it to call BuildParameters in the derived class and not the base class, same with endpoint_url. At this point, base.BuildParameters is called instead. How can I do this.
/\ |_ E X E GG
You also haven't marked your inherited method as overrides (although i've spent all day coding in VB.net and may not have that quite right...)