473,325 Members | 2,860 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,325 software developers and data experts.

copying objects

This seems real simple but I don't know how to do it. Obviously, what I'm
looking for isn't what I typed.

I don't want the assignment operator in commit() and reset() to work by
reference but System.Object doesn't have a copy or clone method available.
I'm sure I'm missing something simple.

public class MyProperty {
private object currVal;
private object newVal;

public MyProperty(object initialValue) {}

public abstract object Value { get; set; }
public void commit () { currVal = newVal; }
public void reset () { newVal = currVal; }

}
Nov 17 '05 #1
5 1808

If you want "copy by value" aka "deep copy" semantincs implement ICloneable
on your objects:

http://msdn.microsoft.com/library/de...classtopic.asp

Somewhere in MSDN there is a good article about proper implementation of a
C# class that can do a deep copy - sorry but I couldn't find it for you.

It's kind of a pain to implement if I remember correctly because you're
supposed to implement ICloneable, define your .Clone() method with specific
behaviors, and then make a static form of an equality operator to test if two
objects are equal. -- If you do all of those steps then in code you write:
currVal = newVal.Clone()...


"Bob Weiner" wrote:
This seems real simple but I don't know how to do it. Obviously, what I'm
looking for isn't what I typed.

I don't want the assignment operator in commit() and reset() to work by
reference but System.Object doesn't have a copy or clone method available.
I'm sure I'm missing something simple.

public class MyProperty {
private object currVal;
private object newVal;

public MyProperty(object initialValue) {}

public abstract object Value { get; set; }
public void commit () { currVal = newVal; }
public void reset () { newVal = currVal; }

}

Nov 17 '05 #2
It appears this solution only moves the problem from my current methods
(commit and reset) into another method named Clone().

currVal and newVal will be simple types (ie. long, string, date...) though
that will be defined in whatever class derives from this one. If I knew the
type I could unbox it, copy the value, then box it back up. Since I don't
know the type - nor do I want to unnesessarily constrain future types - this
isn't possible.

Is there no way to dereference the object currVal, copy whatever is found
into the space referenced by newVal?


"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...

If you want "copy by value" aka "deep copy" semantincs implement
ICloneable
on your objects:

http://msdn.microsoft.com/library/de...classtopic.asp

Somewhere in MSDN there is a good article about proper implementation of a
C# class that can do a deep copy - sorry but I couldn't find it for you.

It's kind of a pain to implement if I remember correctly because you're
supposed to implement ICloneable, define your .Clone() method with
specific
behaviors, and then make a static form of an equality operator to test if
two
objects are equal. -- If you do all of those steps then in code you
write:
currVal = newVal.Clone()...


"Bob Weiner" wrote:
This seems real simple but I don't know how to do it. Obviously, what
I'm
looking for isn't what I typed.

I don't want the assignment operator in commit() and reset() to work by
reference but System.Object doesn't have a copy or clone method
available.
I'm sure I'm missing something simple.

public class MyProperty {
private object currVal;
private object newVal;

public MyProperty(object initialValue) {}

public abstract object Value { get; set; }
public void commit () { currVal = newVal; }
public void reset () { newVal = currVal; }

}

Nov 17 '05 #3

Check out the ICloneable interface and examples.

m

"Bob Weiner" <bo*@engr.uconn.edu> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
This seems real simple but I don't know how to do it. Obviously, what I'm
looking for isn't what I typed.

I don't want the assignment operator in commit() and reset() to work by
reference but System.Object doesn't have a copy or clone method available.
I'm sure I'm missing something simple.

public class MyProperty {
private object currVal;
private object newVal;

public MyProperty(object initialValue) {}

public abstract object Value { get; set; }
public void commit () { currVal = newVal; }
public void reset () { newVal = currVal; }

}

Nov 17 '05 #4
currVal and newVal will be simple types (ie. long, string, date...)
If they are all value types and immutable as you state, then there is no
reason to copy them, as they have no state and cannot change between
commit() and reset(). (Unless I'm not understanding what you're saying.)

If they aren't immutable, then you could copy via:
(a) ICloneable
(b) Refection calls
(c) A switch of known Types/Classes
(d) probably other ways

m
"Bob Weiner" <bo*@engr.uconn.edu> wrote in message
news:O1**************@TK2MSFTNGP10.phx.gbl... It appears this solution only moves the problem from my current methods
(commit and reset) into another method named Clone().

currVal and newVal will be simple types (ie. long, string, date...) though
that will be defined in whatever class derives from this one. If I knew
the type I could unbox it, copy the value, then box it back up. Since I
don't know the type - nor do I want to unnesessarily constrain future
types - this isn't possible.

Is there no way to dereference the object currVal, copy whatever is found
into the space referenced by newVal?


"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...

If you want "copy by value" aka "deep copy" semantincs implement
ICloneable
on your objects:

http://msdn.microsoft.com/library/de...classtopic.asp

Somewhere in MSDN there is a good article about proper implementation of
a
C# class that can do a deep copy - sorry but I couldn't find it for you.

It's kind of a pain to implement if I remember correctly because you're
supposed to implement ICloneable, define your .Clone() method with
specific
behaviors, and then make a static form of an equality operator to test if
two
objects are equal. -- If you do all of those steps then in code you
write:
currVal = newVal.Clone()...


"Bob Weiner" wrote:
This seems real simple but I don't know how to do it. Obviously, what
I'm
looking for isn't what I typed.

I don't want the assignment operator in commit() and reset() to work by
reference but System.Object doesn't have a copy or clone method
available.
I'm sure I'm missing something simple.

public class MyProperty {
private object currVal;
private object newVal;

public MyProperty(object initialValue) {}

public abstract object Value { get; set; }
public void commit () { currVal = newVal; }
public void reset () { newVal = currVal; }

}


Nov 17 '05 #5
I ended up solving the problem as follows. This will let me create a
property containing any simple values, without knowing the value types, and
making updates as necessary. Please let me know if you see any flaws.

Thanks for all the input. It was educational.

--------------------------------------------------------

public abstract class MyProperty {

protected const int current = 0;
protected const int proposed = 1;
protected object[] values = new object[2];

public MyProperty(object myValue) { values[current] = myValue; }

public object Value {
get { return values[proposed]; }
set { values[proposed] = value; }
}

public abstract bool Valid {get;}
public void commit () { if (this.Valid) Array.Copy(values, 1, values, 0,
1); }
public void reset () { Array.Copy(values, 0, values, 1, 1); }

public void display (string msg) { ... }

}

"Bob Weiner" <bo*@engr.uconn.edu> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
This seems real simple but I don't know how to do it. Obviously, what I'm
looking for isn't what I typed.

I don't want the assignment operator in commit() and reset() to work by
reference but System.Object doesn't have a copy or clone method available.
I'm sure I'm missing something simple.

public class MyProperty {
private object currVal;
private object newVal;

public MyProperty(object initialValue) {}

public abstract object Value { get; set; }
public void commit () { currVal = newVal; }
public void reset () { newVal = currVal; }

}

Nov 17 '05 #6

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

Similar topics

5
by: Thomas Lotze | last post by:
Hi, another question: What's the most efficient way of copying data between two file-like objects? f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a string containing...
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...
13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
6
by: ziwu | last post by:
std::copy() is a function from C++ library. Is memcpy() a function from C library, or is it re-implemented in C++ library? Why people say "In C++, don't use memcpy for non-POD types? What is...
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...
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...
5
by: Frederick Gotham | last post by:
I want to copy an array of objects. If it were C, I'd simply do: memcpy( target_array, source_array, sizeof target_array ); However, this won't suffice for objects in C++.
0
by: RS200Phil | last post by:
Hi all, We have this morning used DTS to copy some objects from one database to another. We removed the tick to "Copy all objects" and selected just the 4 tables that we wanted However, we...
9
by: Julian | last post by:
Hi, I have a vector defined like this: std::vector<Load*LoadList; which is populated with different objects of classes that are derived from 'Load'. I need to make a copy of this list during...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.