467,879 Members | 1,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,879 developers. It's quick & easy.

how to implement a C# copy constructor

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???????????????
}
}

Nov 15 '05 #1
  • viewed: 16677
Share:
4 Replies
Peter wrote:
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)
Nov 15 '05 #2

"Peter" <ab*@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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();

}

}

Nov 15 '05 #3

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/de...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/de...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.

Nov 15 '05 #4

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.

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by Edward Diener | last post: by
15 posts views Thread by A | last post: by
8 posts views Thread by Jesper | last post: by
10 posts views Thread by utab | last post: by
8 posts views Thread by shuisheng | last post: by
4 posts views Thread by shuisheng | last post: by
reply views Thread by jack112 | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.