Copy Entity Based on DataTable?

L

lucius

I have some complex objects that inherit from DataTable. I want to
copy the actual data along with the custom properties and etc, so I
don't think I can use the .Copy() method in DataTable. Do I need to
override it in some way to make sure the object is truly copies to a
new instance when I need it?

Thanks.
 
W

WenYuan Wang [MSFT]

Hello Lucius,

According to your description, you need a method to copy the instance of
class which inherited from DataTable. Did I misunderstand anything here?
Please correct me. Thanks.

We need not override the base class's copy method, because the return type
of the new method is different from the DataTable.Copy method.

public DataTable Copy()
{....}
public table2 Copy()
{....}

What we need to do is to create a new method named Copy and return a
instance of class(inherited from DataTable). We could also get benefit from
base.Copy method. This method will copy all columns and rows from the
original instance. Then, we need set the other properties which we added
into the class, and then return new instance.

For example:
Table2 is a new class which inherited from datatable. I added id and name
as new properties.
class table2 : System.Data.DataTable
{
public int id;
public string name;

Implementation of Copy() method, calls base.Copy to copy all the columns
and rows and then set id and name.
public table2 Copy()
{
table2 t = new table2();
t = base.Copy() as table2;
t.id = this.id;
t.name = this.name;
return t;
}
}

Hope this helps.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

WenYuan Wang [MSFT]

Dear Lucius,

How are you? I just want to check if there is anything we can help with.
Please don't hesitate to let me know if you have anything unclear or any
further issue. We are glad to assist you.

Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
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