473,378 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Copying an object

Hello there! I'm fairly new to C#, so please excuse me if this
question seems somewhat basic. I need a way to copy the fields from
one object into another. It's akin to the Clone() method, however in
this case the destination object already exists and I cannot replace
it with another instance. Something like "public void CloneInto(Object
copy);".

I suppose this can be done manually by using Reflection, but maybe
this problem is already solved in .NET and I just haven't found that
solution...

Best regards,
- Matias
Nov 15 '05 #1
2 1530
How about doing something like this;

public myObject CloneInto(Object copy)
{
Object myObject = new Object()//create a new instance of your object

myObject = copy //Make copy equal to your new object
//you should now have two exact copies (myObject
and copy)

return myObject;

}

hope this helps

Marco
"Matias Hernandez" <m.*********@datalogic.com.uy> wrote in message
news:d6**************************@posting.google.c om...
Hello there! I'm fairly new to C#, so please excuse me if this
question seems somewhat basic. I need a way to copy the fields from
one object into another. It's akin to the Clone() method, however in
this case the destination object already exists and I cannot replace
it with another instance. Something like "public void CloneInto(Object
copy);".

I suppose this can be done manually by using Reflection, but maybe
this problem is already solved in .NET and I just haven't found that
solution...

Best regards,
- Matias

Nov 15 '05 #2
Does your clone need to do a deep copy? If so, read this:

http://weblogs.asp.net/whaggard/arch...3/02/3313.aspx

Also, take note of the article's recommendation to implement ICloneable!
Just cleaner/more uniform that way.

Hope this helps.

Erik

"Matias Hernandez" <m.*********@datalogic.com.uy> wrote in message
news:d6**************************@posting.google.c om...
Hello there! I'm fairly new to C#, so please excuse me if this
question seems somewhat basic. I need a way to copy the fields from
one object into another. It's akin to the Clone() method, however in
this case the destination object already exists and I cannot replace
it with another instance. Something like "public void CloneInto(Object
copy);".

I suppose this can be done manually by using Reflection, but maybe
this problem is already solved in .NET and I just haven't found that
solution...

Best regards,
- Matias

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
2
by: Marijn | last post by:
Say i have an object that represents or holds a resource like an open file, a block of memory on the heap, or an openGL texture object. The constructor acquires the resource, and the destructor...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
22
by: Matt | last post by:
When browsing a web page a user has the ability to highlight content on a page (by holding down the left mouse button and dragging the mouse over the desired content). Is there a way to disable...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
5
by: Steve - DND | last post by:
Is there any way to easily deep copy an entire object? Maybe something with unsafe code to make a full and completely duplicated copy of an object with no reference ties to the original? I want...
6
by: solex | last post by:
Hello, I am trying to use serialization to copy objects. The object in question "Institution" inherits from a parent object "Party" both are marked as <Serializable()>. Initially I can copy an...
1
by: chris.atlee | last post by:
I'm writing a program in python that creates tar files of a certain maximum size (to fit onto CD/DVD). One of the problems I'm running into is that when using compression, it's pretty much...
6
by: kimiraikkonen | last post by:
Hi, I use system.io.file class to copy files but i have a difficulty about implementing a basic / XP-like progress bar indicator during copying process. My code is this with no progress bar,...
2
by: raylopez99 | last post by:
Beware newbies: I spent a day before I figured this out: copying a bitmap (image) file to file is not quite like copying a text file--you have to do some tricks (see below), like using a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.