Derived class constructors
-
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.
-
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.
It's called inheretance and Object-Oriented Programming.
only two letters away from being an asset
-
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.
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 -
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 -
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.
How is this causing you problems? Here is an article explaing how inheritance works: Introduction to inheritance, polymorphism in C#[^]
topcoderjax
-
How is this causing you problems? Here is an article explaing how inheritance works: Introduction to inheritance, polymorphism in C#[^]
topcoderjax
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.
-
The GetChanges function has DataTable as a return type, so yes, at some point the constructor for the DataTable type will be called.
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 -
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.
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
-
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