how to implement a C# copy constructor

P

Peter

I want to copy a parent class instance's all datas to a child's. It's
actually a C++'s copy constructor. But why the following code does not
work - there is a compile error! How it should look like?

(The background is I don't know (I don't care indeed) all members in
DataGrid, so I don't want to copy all members in DataGrid one by one.)

public class GridEx : DataGrid
{
public GridEx()
{
InitializeComponent();
this.HeaderClicked += new HeaderEventHandler(
InternalHeaderClick );
}

public GridEx( DataGrid src )
: this()
{
base = src.MemberwiseClone(); //error CS0175: Use of keyword base
is not valid in this context
// Why? and how
should I to do??????????
// OR
this = src.MemberwiseClone(); //error CS1540: Cannot access
protected member
//'object.MemberwiseClone()'
via a qualifier of type 'DataGrid';
//the qualifier must be of
type 'GridEx' (or derived from it)

// What's the solution for the issue???????????????
}
}
 
F

Frank Oquendo

Peter said:
base = src.MemberwiseClone(); //error CS0175: Use of
keyword base is not valid in this context
// Why?

base is a keyword indicating the invocation of a member belonging to a
type's base class. Use a different variable name.
this = src.MemberwiseClone(); //error CS1540: Cannot
access protected member

//'object.MemberwiseClone()' via a qualifier of type 'DataGrid';
//the qualifier must
be of type 'GridEx' (or derived from it)

// What's the solution for the issue???????????????

According to the code, you're trying to define a conversion operator not a
copy constructor.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
D

Daniel O'Connell [C# MVP]

Peter said:
I want to copy a parent class instance's all datas to a child's. It's
actually a C++'s copy constructor. But why the following code does not
work - there is a compile error! How it should look like?

(The background is I don't know (I don't care indeed) all members in
DataGrid, so I don't want to copy all members in DataGrid one by one.)

public class GridEx : DataGrid
{
public GridEx()
{
InitializeComponent();
this.HeaderClicked += new HeaderEventHandler(
InternalHeaderClick );
}

public GridEx( DataGrid src )
: this()
{
base = src.MemberwiseClone(); //error CS0175: Use of keyword base
is not valid in this context
// Why? and how
should I to do??????????
// OR
this = src.MemberwiseClone(); //error CS1540: Cannot access
protected member

//'object.MemberwiseClone()'
via a qualifier of type 'DataGrid';
//the qualifier must be of
type 'GridEx' (or derived from it)

// What's the solution for the issue???????????????
}
}

As a whole, the copy constructor pattern doesn't work as it does in C++. You
are better off using ICloneable and the .Clone() method instead of trying to
make copy constructors work.

public class MyClass : ICloneable

{

public object Clone()

{

return this.MemberwiseClone();

}

}
 
G

Guest

Hi Peter,

Thank you for posting in the community!

Based on my understanding, you want to implement an extended datagrid in
C#, and you want to initialize your extended datagrid with an existed
normal datagrid instance.
==========================================
In your description "copy a parent class instance's all datas to a
child's", what exactly does "all data" mean? Does it mean "Shadow Copy" or
"Deep Copy"?

To see the difference between "Shadow Copy" and "Deep Copy", please refer
to "Remarks" section in below link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemobjectclassmemberwiseclonetopic.asp

Based on your code snippet, I think you want to implement "Shadow
Copy"(Because you use MemberwiseClone method)

Because MemberwiseClone is a protected member of object class, you can not
call it outside of this class or its child class. (That's why the error
message generates)

Just as Daniel said, to do clone in .Net, you may implement the ICloneable
interface, this interface only takes one method "Clone", which you can do
your copy implement. Normally, to do Shadow Copy, you can just call the
object.MemberwiseClone in the interface's implement.
In "ICloneable Interface" below, you will see that, DataGrid class does not
implement ICloneable interface, so you must implement this interface
yourself:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemicloneableclasstopic.asp

As a whole, you should do like this:
public class MyDataGrid : ICloneable, DataGrid
{
public object Clone()
{
return this.MemberwiseClone();
}
}

public class extendeddatagrid: DataGrid
{
public extendeddatagrid(MyDataGrid obj)
{
this=obj.Clone() as DataGrid;
}
}

Note: in the constructor parameter, you must use the class inherited from
DataGrid, which may not meet your need(You want to take the Normal DataGrid
as parameter), if you really want to use normal DataGrid as parameter, you
must do the Shadow Copy yourself in the constructor, which may be
troublesome.

=================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi Peter,

Does my reply make sense to you?

If you still have anything unclear, please feel free to tell me, I will
help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top