PC Review


Reply
Thread Tools Rate Thread

Cloning subclasses. How does casting to a derived class work?

 
 
Heinz Kiosk
Guest
Posts: n/a
 
      16th Jan 2004
A piece of code generated by an MS wizard goes as follows:

public class MyDataTable : DataTable...
{
....
public override DataTable Clone()
{
MyDataTable cln = ((MyDataTable)(base.Clone())); // What is going on here?
cln.InitVars();
return cln;
}
....
}

MyDataTable is derived from DataTable. How does casting a DataTable to a
MyDataTable in the commented line work? I must be missing some rules about
how casting works in .NET. If you cast to a derived class does the compiler
fill in defaults for derived members or use the derived constructor and then
do a field-by-field copy or what? Something clever must be going on.

Thank you.

Tom


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      16th Jan 2004
Heinz Kiosk <(E-Mail Removed)> wrote:
> A piece of code generated by an MS wizard goes as follows:
>
> public class MyDataTable : DataTable...
> {
> ...
> public override DataTable Clone()
> {
> MyDataTable cln = ((MyDataTable)(base.Clone())); // What is going on here?
> cln.InitVars();
> return cln;
> }
> ...
> }
>
> MyDataTable is derived from DataTable. How does casting a DataTable to a
> MyDataTable in the commented line work? I must be missing some rules about
> how casting works in .NET. If you cast to a derived class does the compiler
> fill in defaults for derived members or use the derived constructor and then
> do a field-by-field copy or what? Something clever must be going on.


base.Clone() is actually returning a reference to a MyDataTable object
- at least if it's working according to the documentation. It's
probably using object.MemberwiseCopy() - have a look at the description
for that method for more information. Hope that explains what's going
on - let me know if you have any more concerns.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Heinz Kiosk
Guest
Posts: n/a
 
      16th Jan 2004
"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Heinz Kiosk <(E-Mail Removed)> wrote:
> > A piece of code generated by an MS wizard goes as follows:
> >
> > public class MyDataTable : DataTable...
> > {
> > ...
> > public override DataTable Clone()
> > {
> > MyDataTable cln = ((MyDataTable)(base.Clone())); // What is going on

here?
> > cln.InitVars();
> > return cln;
> > }
> > ...
> > }
> >
> > MyDataTable is derived from DataTable. How does casting a DataTable to a
> > MyDataTable in the commented line work? I must be missing some rules

about
> > how casting works in .NET. If you cast to a derived class does the

compiler
> > fill in defaults for derived members or use the derived constructor and

then
> > do a field-by-field copy or what? Something clever must be going on.

>
> base.Clone() is actually returning a reference to a MyDataTable object
> - at least if it's working according to the documentation. It's
> probably using object.MemberwiseCopy() - have a look at the description
> for that method for more information. Hope that explains what's going
> on - let me know if you have any more concerns.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too


Thanks for that jon, the implementation of Clone is so alien to my 10 years
of C++ experience that I somehow missed the comment about subclasses in the
documentation, because I would just have viewed that as impossible.... These
classes that know all about their storage and implementation and the storage
etc derivations take some getting used to. Presumably the ability to clone
controls works similarly.

Thanks

Tom


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to avoid casting horror with non-generic class derived fromgeneric class John Rivers Microsoft C# .NET 5 21st Dec 2009 01:20 PM
Casting a derived class to base class user Microsoft C# .NET 2 11th Dec 2008 04:12 PM
Casting a C# base class object to a derived class type. =?Utf-8?B?Q2hyaXMgQ2Fwb24=?= Microsoft C# .NET 5 3rd Jun 2005 11:35 PM
casting to a derived class =?Utf-8?B?U3RldmUgVGVlcGxlcw==?= Microsoft C# .NET 12 18th May 2005 05:41 PM
Casting a base class to a derived class =?Utf-8?B?TWFyayBNY0RvbmFsZA==?= Microsoft C# .NET 2 10th Jan 2005 04:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:33 PM.