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. Derived class constructors

Derived class constructors

Scheduled Pinned Locked Moved C#
tutorialquestion
9 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.
  • J Offline
    J Offline
    Johnny 0
    wrote on last edited by
    #1

    I have a situation I am not sure how to deal with. I have a class derived from the DataTable class When I call the GetChanges form within an instance of the derived class the constructor of the derived class is called instead of just the DataTable constructor. this.GetChanges(); I was expecting that only the Datatable constructor would be called. Is there a way around this ? I tried calling base.GeatChanges(); but same result.

    N D D 3 Replies Last reply
    0
    • J Johnny 0

      I have a situation I am not sure how to deal with. I have a class derived from the DataTable class When I call the GetChanges form within an instance of the derived class the constructor of the derived class is called instead of just the DataTable constructor. this.GetChanges(); I was expecting that only the Datatable constructor would be called. Is there a way around this ? I tried calling base.GeatChanges(); but same result.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      It's called inheretance and Object-Oriented Programming.


      only two letters away from being an asset

      1 Reply Last reply
      0
      • J Johnny 0

        I have a situation I am not sure how to deal with. I have a class derived from the DataTable class When I call the GetChanges form within an instance of the derived class the constructor of the derived class is called instead of just the DataTable constructor. this.GetChanges(); I was expecting that only the Datatable constructor would be called. Is there a way around this ? I tried calling base.GeatChanges(); but same result.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Since when is the method GetChanges a constructor for the DataTable class?? Or any class for that matter?

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        J 1 Reply Last reply
        0
        • D Dave Kreskowiak

          Since when is the method GetChanges a constructor for the DataTable class?? Or any class for that matter?

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          J Offline
          J Offline
          Johnny 0
          wrote on last edited by
          #4

          The GetChanges function has DataTable as a return type, so yes, at some point the constructor for the DataTable type will be called.

          D 1 Reply Last reply
          0
          • J Johnny 0

            I have a situation I am not sure how to deal with. I have a class derived from the DataTable class When I call the GetChanges form within an instance of the derived class the constructor of the derived class is called instead of just the DataTable constructor. this.GetChanges(); I was expecting that only the Datatable constructor would be called. Is there a way around this ? I tried calling base.GeatChanges(); but same result.

            D Offline
            D Offline
            Dave Herren
            wrote on last edited by
            #5

            How is this causing you problems? Here is an article explaing how inheritance works: Introduction to inheritance, polymorphism in C#[^]

            topcoderjax

            J 1 Reply Last reply
            0
            • D Dave Herren

              How is this causing you problems? Here is an article explaing how inheritance works: Introduction to inheritance, polymorphism in C#[^]

              topcoderjax

              J Offline
              J Offline
              Johnny 0
              wrote on last edited by
              #6

              I don't have a problem understanding inheritence What I am not sure is why since the return type of the GetChanges is DataTable, I expect the DataTable contructor to be called but if I call that same function form a Class derived form DataTable all of a sudden the constructor of the dirived class is called although the return type is still a DataTable, I didn't touch that function. Here is the situation as clear as I can make it: class myType : DataTable { myType():base() {} public someFunction() { code.... DataTable someDataTable = this.GetChanges(); } In the last line a myType seems to be created by the Activator, I would have expected it to create a DataTable, nothing more.

              D 1 Reply Last reply
              0
              • J Johnny 0

                The GetChanges function has DataTable as a return type, so yes, at some point the constructor for the DataTable type will be called.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                True, mostly. The constructor for A class will be called. How about base.GetChanges().

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007

                1 Reply Last reply
                0
                • J Johnny 0

                  I don't have a problem understanding inheritence What I am not sure is why since the return type of the GetChanges is DataTable, I expect the DataTable contructor to be called but if I call that same function form a Class derived form DataTable all of a sudden the constructor of the dirived class is called although the return type is still a DataTable, I didn't touch that function. Here is the situation as clear as I can make it: class myType : DataTable { myType():base() {} public someFunction() { code.... DataTable someDataTable = this.GetChanges(); } In the last line a myType seems to be created by the Activator, I would have expected it to create a DataTable, nothing more.

                  D Offline
                  D Offline
                  Dave Herren
                  wrote on last edited by
                  #8

                  Microsoft documentation says the GetChanges method: Gets a copy of the DataTable containing all changes made to it since it was last loaded, or since AcceptChanges was called. My guess is that it is using DataTable.Clone and the documentation on DataTable.Clone notes that "If these classes have been derived, the clone will also be of the same derived classes". See Documentation.[^] So it is copying your instance (which is of MyType) and returning it as a DataTable. Overriding DataTable.Clone or GetChanges might fix your problem.

                  topcoderjax

                  J 1 Reply Last reply
                  0
                  • D Dave Herren

                    Microsoft documentation says the GetChanges method: Gets a copy of the DataTable containing all changes made to it since it was last loaded, or since AcceptChanges was called. My guess is that it is using DataTable.Clone and the documentation on DataTable.Clone notes that "If these classes have been derived, the clone will also be of the same derived classes". See Documentation.[^] So it is copying your instance (which is of MyType) and returning it as a DataTable. Overriding DataTable.Clone or GetChanges might fix your problem.

                    topcoderjax

                    J Offline
                    J Offline
                    Johnny 0
                    wrote on last edited by
                    #9

                    Makes sense, thanks for the input

                    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