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. Web Development
  3. Help me impliment this class structure

Help me impliment this class structure

Scheduled Pinned Locked Moved Web Development
jsonhelpquestion
3 Posts 3 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 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

    K P 2 Replies Last reply
    0
    • E eggie5

      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

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      Try overriding the base class save in the child class and perhaps marking the base class save as abstract. That will force the child classes to override it. Hope that helps. Ben

      1 Reply Last reply
      0
      • E eggie5

        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

        P Offline
        P Offline
        Paddy Boyd
        wrote on last edited by
        #3

        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...)

        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