In the C# , I want to change the object only after the user click the
"Submit" button , so I first new an object and use the "=" to get the object
in memory , I found that the operation "=" only get a reference to the
target object which I want to copy , how can I do copy it ? 11 5188
"DogEye" <ji*****@tom.co m> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ: news:eX******** ******@TK2MSFTN GP15.phx.gbl... In the C# , I want to change the object only after the user click the "Submit" button , so I first new an object and use the "=" to get the object in memory , I found that the operation "=" only get a reference to the target object which I want to copy , how can I do copy it ?
If the object type supports copying (cloning) it implements the ICloneable
interface.
// begin
String s1 = "Original string";
String s2 = (String) s1.Clone(); // this must be a copy of the original string
s1 = "Another string";
Console.WriteLi ne(s1);
Console.WriteLi ne(s2);
// end
Sergei
DogEye wrote: In the C# , I want to change the object only after the user click the "Submit" button , so I first new an object and use the "=" to get the
object in memory , I found that the operation "=" only get a reference to the target object which I want to copy , how can I do copy it ?
Why do you want to copy the object you just created ? So you'd create a new
object and keep a copy of that in memory ? What are you going to do with the
original objcet ?
1. This example is complete nonsense, since a string is a specialised object
and is immutable and therefore just by "referencin g" another instance you
are effectively making a copy. See thus...
string s1 = "two";
string s2 = s1;
s1 = "one";
System.Diagnost ics.Debug.Write Line(string.For mat("s1 {0}, s2
{1}",s1,s2));
2. DogEye, Although in essence Sergei's technique for copying an object is
correct, please note that available to you are Copy and Clone methods to
make shallow and deep copies. Unfortunately (I seem to remember) there is a
little inconsistancy of their actions in the Framework - so for more complex
objects you should make sure to check the documentation to see what
functionality you are getting before assuming it.
Br,
Mark.
"Sergei" <se****@nospam. summertime.mtu-net.ru> wrote in message
news:OA******** ******@TK2MSFTN GP12.phx.gbl... "DogEye" <ji*****@tom.co m> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ: news:eX******** ******@TK2MSFTN GP15.phx.gbl... In the C# , I want to change the object only after the user click the "Submit" button , so I first new an object and use the "=" to get the object in memory , I found that the operation "=" only get a reference to the target object which I want to copy , how can I do copy it ?
If the object type supports copying (cloning) it implements the ICloneable interface.
// begin String s1 = "Original string"; String s2 = (String) s1.Clone(); // this must be a copy of the original string s1 = "Another string"; Console.WriteLi ne(s1); Console.WriteLi ne(s2); // end
Sergei
In C# there are reference types and value types. Classes are reference types
and structs as well as basic types are value types.
If you want to copy a class, you can't use the assignment operator. You must
use some other method. Many classes will implement mechanisms to perform
this action. The framework defines an interface ICloneable that is
implemented by many framework classes. However, ICloneable doesn't dictate
weather a deep or shallow copy is made. You will have to look to the
documentation for a particular class to see for sure. A deep copy makes full
copies of all state information including other objects that are members of
the class being copied. In a shallow copy all state is copied but references
to member objects are maintained.
"DogEye" <ji*****@tom.co m> wrote in message
news:eX******** ******@TK2MSFTN GP15.phx.gbl... In the C# , I want to change the object only after the user click the "Submit" button , so I first new an object and use the "=" to get the object in memory , I found that the operation "=" only get a reference to the target object which I want to copy , how can I do copy it ?
On Fri, 10 Jun 2005 12:25:05 +0100, Mark Broadbent wrote: 1. This example is complete nonsense, since a string is a specialised object and is immutable and therefore just by "referencin g" another instance you are effectively making a copy. See thus... string s1 = "two"; string s2 = s1; s1 = "one"; System.Diagnost ics.Debug.Write Line(string.For mat("s1 {0}, s2 {1}",s1,s2));
Does your example support what you said that 's2 = s1' causes a copy of s1
to be made?
As in, new memory allocated, content of s1 copied over to new memory,
address of new memory written in s2 ?
How do we know that s2 isn't still pointing to the memory region initially
allocated for s1 ?
I would think that only knowing the addresses stored in s1 and s2 would
prove what 'referencing' does and what Clone() does.
Rico.
Rico <ra*****@yahoo. com> wrote: 1. This example is complete nonsense, since a string is a specialised object and is immutable and therefore just by "referencin g" another instance you are effectively making a copy. See thus... string s1 = "two"; string s2 = s1; s1 = "one"; System.Diagnost ics.Debug.Write Line(string.For mat("s1 {0}, s2 {1}",s1,s2)); Does your example support what you said that 's2 = s1' causes a copy of s1 to be made?
No - in fact a copy isn't being made, but because strings are immutable
it *sort* of looks like it is. Personally I don't like talking about
strings as if they were special - they have *some* special handling
(for literals, basically) but there aren't many things about them which
are different to any other immutable class.
As in, new memory allocated, content of s1 copied over to new memory, address of new memory written in s2 ?
Nope.
How do we know that s2 isn't still pointing to the memory region initially allocated for s1 ?
It is, in fact.
I would think that only knowing the addresses stored in s1 and s2 would prove what 'referencing' does and what Clone() does.
In fact, using object.Referenc eEquals you can tell that string.Clone()
just returns "this":
using System;
public class Test
{
static void Main()
{
string x = "hello";
string y = (string)x.Clone ();
Console.WriteLi ne (object.Referen ceEquals(x, y));
}
}
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Really not sure what you are trying to say?
As my example demonstrates Sergei's use of the string type in this scenario
is not valid. As I said in my post since string is immutable, it is
impossible for one string to change the an instance of another string. This
is fact and is pointless debating, so proving that s1 doesnt point to s2 is
not required.
You'll also note that I said "referencin g another instance you
are effectively making a copy" in this string scenario. What I mean by
'effectively' is that even *if* s1 did point to s2 at that time of
assignment, the exact moment that a change is made to either s1 or s2 they
would point to different instances due to the immutable rule (as my simple
example shows).
What is happening is not the same as what you say
'As in, new memory allocated, content of s1 copied over to new memory,
address of new memory written in s2 ?'
to put it simply, the value that is referenced by a string variable is
protected from a change from another.
br,
Mark.
"Rico" <ra*****@yahoo. com> wrote in message
news:pa******** *************** *****@yahoo.com ... On Fri, 10 Jun 2005 12:25:05 +0100, Mark Broadbent wrote:
1. This example is complete nonsense, since a string is a specialised object and is immutable and therefore just by "referencin g" another instance you are effectively making a copy. See thus... string s1 = "two"; string s2 = s1; s1 = "one"; System.Diagnost ics.Debug.Write Line(string.For mat("s1 {0}, s2 {1}",s1,s2));
Does your example support what you said that 's2 = s1' causes a copy of s1 to be made? As in, new memory allocated, content of s1 copied over to new memory, address of new memory written in s2 ?
How do we know that s2 isn't still pointing to the memory region initially allocated for s1 ?
I would think that only knowing the addresses stored in s1 and s2 would prove what 'referencing' does and what Clone() does.
Rico.
Since you ask if the assignment operator makes a copy I am assuming that
you may come from C++. The _key_ concept here is that in C++ objects
have
_value_ semantics so that the assignment operator may make a copy. In
C#,
classes have reference semantics. IMHO, it is best to adjust to the use
of C#
reference semantics and garbage collection. If you want value semantics
and
if you want copy on assignment behaviour then you can use struct in C#.
So to really understand your need, it would be helpful to know why you
need
a copy of the object, rather than just changing the state of the object
using
the reference. Strings are immutable objects, so you cannot change the
state
of a string, but you can create a new string and assign it to an
existing
reference variable. This is done automagically without a call to new.
Regards,
Jeff
*** Sent via Developersdex http://www.developersdex.com ***
"Mark Broadbent" <no****@nospam. com> wrote: 1. This example is complete nonsense, since a string is a specialised object and is immutable
Yes, I shouldn't use the String type in the example
because its Clone() doesn't clone.
Sergei
and therefore just by "referencin g" another instance you are effectively making a copy. See thus... string s1 = "two"; string s2 = s1; s1 = "one"; System.Diagnost ics.Debug.Write Line(string.For mat("s1 {0}, s2 {1}",s1,s2));
2. DogEye, Although in essence Sergei's technique for copying an object is correct, please note that available to you are Copy and Clone methods to make shallow and deep copies. Unfortunately (I seem to remember) there is a little inconsistancy of their actions in the Framework - so for more complex objects you should make sure to check the documentation to see what functionality you are getting before assuming it.
Br,
Mark. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 object from a copy of another object of the same kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn't
mean that they couldn't be copied from another object of the same kind when...
|
by: MSR |
last post by:
I have a couple of questions.
1. Copy Constructor.
class A {
private:
int a1;
double d1;
char *ptr;
|
by: Tony Johansson |
last post by:
Hello!
I'm reading in a book about C++ and that is something that sound strange.
It says "Pointers have reference-assignment semantics similar to those in
Java. For example, after the assignment
Student* john = michael;
both john and michael share the same object. This type of an assignment is
different then value-assignmnet semantics used by class variables, as in
|
by: Harag |
last post by:
Hi all
If I create an object with the following:
var ob1 = new objMyDefinedObject();
then I assign it to a new variable.
var ob2 = ob1
|
by: lion |
last post by:
in .net, if you set annstance-A of a class equal to another instance-B, a
pointer will add to B, but if i want to create a copy of B instead of
pointer, how to operate?
Note:serialization isn't permitted
3x
| |
by: OpticTygre |
last post by:
If I pass an object to another form via the new form's tag property, I want
to create an object exactly like it, with it's properties and all, but have
it be a copy of the object passed through, and not just a reference to the
object.
Example:
Public Sub Form_Load(ByVal sender as Object, ByVal e as EventArgs)
Dim obj as Object = Activator.CreateInstance(Me.Tag.GetType())
|
by: joel.winteregg |
last post by:
Hi all,
I "learnt" C++ a few years ago and then i have been using C for a
couple of month and i'm now trying to get back to the ++ world (but
with some troubles...). I have some problem to understand why a "new"
return a pointer and not a reference. Here are explanations:
/* to get my object in heap */
|
by: Mohan |
last post by:
Hi,
What are the advantages/disadvantages of using a pointer instead of
Reference in the Copy Constructor ?
For Example,
Writing the Copy constructor for the Class "Temp" as below,
Temp(const base *ptrBase)
|
by: rksadhi |
last post by:
/*Geting error ---object reference not set to an instance---at bold line----plz reply asap thanks in advance*/
cmd = new OleDbCommand ("SELECT e.emp_id,e.email, m.email AS Email FROM emp_details e INNER JOIN emp_details m ON e.spid = m.emp_id AND e.emp_id='" + e_id + "'",conn);
dr=cmd.ExecuteReader();
if(dr.Read())
{
strmail=dr.ToString();
strmailto=dr.ToString();
}
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |