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. Simple Inheritance

Simple Inheritance

Scheduled Pinned Locked Moved C#
questionoop
5 Posts 4 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 ServiceResponse that inherits WebResponse. When I create a new ServiceResponse I want to set the base WebResponse with a variable that I pass to the Service Response constructor. How can I do this? I have this class structure:

    public class ServiceResponse : WebResponse
    {
    public ServiceResponse(HttpWebResponse resp)
    {
    //how do I set the base WebResponse to resp?
    }
    }

    /\ |_ E X E GG

    C G P 3 Replies Last reply
    0
    • E eggie5

      I have a class ServiceResponse that inherits WebResponse. When I create a new ServiceResponse I want to set the base WebResponse with a variable that I pass to the Service Response constructor. How can I do this? I have this class structure:

      public class ServiceResponse : WebResponse
      {
      public ServiceResponse(HttpWebResponse resp)
      {
      //how do I set the base WebResponse to resp?
      }
      }

      /\ |_ E X E GG

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

      public ServiceResponse(HttpWebResponse resp) : base(resp) { //how do I set the base WebResponse to resp? } this forces a base constructor that takes this parameter to be called. I'd be surprised if you need this code, I'd expect the base constructor to work, as in ServiceResponse sr = new ServiceResponse(resp); // I think this should call the base constructor for you but build the derived object )

      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 )

      1 Reply Last reply
      0
      • E eggie5

        I have a class ServiceResponse that inherits WebResponse. When I create a new ServiceResponse I want to set the base WebResponse with a variable that I pass to the Service Response constructor. How can I do this? I have this class structure:

        public class ServiceResponse : WebResponse
        {
        public ServiceResponse(HttpWebResponse resp)
        {
        //how do I set the base WebResponse to resp?
        }
        }

        /\ |_ E X E GG

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        You can't do that. When the constructor is running, the object is already created, and you are in it. When you create an instance of an inherited class, there is just that instance. There is no separate base object. Perhaps you want to encapsulate an instance of the WebResponse class in your class, instead of inheriting from it.

        --- single minded; short sighted; long gone;

        E 1 Reply Last reply
        0
        • G Guffa

          You can't do that. When the constructor is running, the object is already created, and you are in it. When you create an instance of an inherited class, there is just that instance. There is no separate base object. Perhaps you want to encapsulate an instance of the WebResponse class in your class, instead of inheriting from it.

          --- single minded; short sighted; long gone;

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

          Guffa wrote:

          Perhaps you want to encapsulate an instance of the WebResponse class in your class, instead of inheriting from it.

          Yeah, I just did this and it seems like a better fit.

          /\ |_ E X E GG

          1 Reply Last reply
          0
          • E eggie5

            I have a class ServiceResponse that inherits WebResponse. When I create a new ServiceResponse I want to set the base WebResponse with a variable that I pass to the Service Response constructor. How can I do this? I have this class structure:

            public class ServiceResponse : WebResponse
            {
            public ServiceResponse(HttpWebResponse resp)
            {
            //how do I set the base WebResponse to resp?
            }
            }

            /\ |_ E X E GG

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Are you looking for something like this?

            public class ServiceResponse : WebResponse
            {
            public ServiceResponse
            (
            HttpWebResponse resp
            )
            : base ( resp )
            {
            }
            }

            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