|
| how to implement a C# copy constructor |
|
|
|
|
| Messages |
|
Related Types |
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| 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??????????????? } }
|
|
|
| |
|
| |
| |
| Daniel O'Connell [C# MVP] (VIP) |
"Peter" <Click here to reveal e-mail address> wrote in message news:%Click here to reveal e-mail address... [Original message clipped]
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();
}
} [Original message clipped]
|
|
|
| |
|
|
| |
|
| |
| Frank Oquendo |
Peter wrote:
[Original message clipped]
base is a keyword indicating the invocation of a member belonging to a type's base class. Use a different variable name.
[Original message clipped]
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)
|
|
|
| |
|
|
| |
|
| |
| Jeffrey Tan[MSFT] (VIP) |
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.
|
|
|
| |
|
|
| |
|
| |
| Jeffrey Tan[MSFT] (VIP) |
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.
|
|
|
| |
|
|
| |
|
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|