472,325 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 software developers and data experts.

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
4 16999
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition....
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B&...
4
by: shuisheng | last post by:
Dear All, Is there any easy way to make sure all my object copies are deep copy or shallow copy? I do not like to implement it in each class one...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector {...
2
by: ecestd | last post by:
how do you implement a copy constructor for this pointer-based ADT queue #include "Queuep.h" #include <cassert> #include <new> using namespace...
2
by: ecestd | last post by:
how do you implement a copy constructor for this pointer-based ADT queue #include <cassert // for assert #include <new // for bad_alloc ...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.