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. Common tasks in base classes, how?

Common tasks in base classes, how?

Scheduled Pinned Locked Moved C#
question
5 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.
  • M Offline
    M Offline
    moazzamahmed
    wrote on last edited by
    #1

    ok next q ... how do you access inputs into a constructor within the base class? my base class constructor: protected int commonID; // this is common to all object public BaseClass{ console.write commonID; // or something that accesses id } my sub-class constrictor: public myClass(int id) { commonID = id; } but when I instantiate "myClass", before it sets commonID, it goes to the BaseClass constructor. But commonID is null cause it hasnt been set. So is there a way for me to get "id" into the baseclass? or get commonID set? M

    C J 2 Replies Last reply
    0
    • M moazzamahmed

      ok next q ... how do you access inputs into a constructor within the base class? my base class constructor: protected int commonID; // this is common to all object public BaseClass{ console.write commonID; // or something that accesses id } my sub-class constrictor: public myClass(int id) { commonID = id; } but when I instantiate "myClass", before it sets commonID, it goes to the BaseClass constructor. But commonID is null cause it hasnt been set. So is there a way for me to get "id" into the baseclass? or get commonID set? M

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Here are your two constructors rewritten:

      public BaseClass(int id)
      {
      commonID = id;
      console.Write(commonID);
      }

      public myClass (int id) : base(id)
      {
      }


      "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

      M 1 Reply Last reply
      0
      • M moazzamahmed

        ok next q ... how do you access inputs into a constructor within the base class? my base class constructor: protected int commonID; // this is common to all object public BaseClass{ console.write commonID; // or something that accesses id } my sub-class constrictor: public myClass(int id) { commonID = id; } but when I instantiate "myClass", before it sets commonID, it goes to the BaseClass constructor. But commonID is null cause it hasnt been set. So is there a way for me to get "id" into the baseclass? or get commonID set? M

        J Offline
        J Offline
        junglerover77
        wrote on last edited by
        #3

        base class: protected int commonID; // this is common to all object public BaseClass{ } public void DoOtherThing { console.write commonID; // or something that accesses id } /////////////////////////////////////////////// sub-class: public myClass(int id) { commonID = id; DoOtherThing(); } Was it a cat I saw?

        C 1 Reply Last reply
        0
        • C Colin Angus Mackay

          Here are your two constructors rewritten:

          public BaseClass(int id)
          {
          commonID = id;
          console.Write(commonID);
          }

          public myClass (int id) : base(id)
          {
          }


          "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

          M Offline
          M Offline
          moazzamahmed
          wrote on last edited by
          #4

          dude, that was perfect, it worked. thanks! Moazzam

          1 Reply Last reply
          0
          • J junglerover77

            base class: protected int commonID; // this is common to all object public BaseClass{ } public void DoOtherThing { console.write commonID; // or something that accesses id } /////////////////////////////////////////////// sub-class: public myClass(int id) { commonID = id; DoOtherThing(); } Was it a cat I saw?

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            But with this code the purpose of having the base class is somewhat lost as your method requires the subclasses to specifically call DoOtherThing() rather than properly inherite the behaviour of the base constructor.


            "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

            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