473,796 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to copy an object instead of geting the reference

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 ?
Nov 17 '05
11 5189
On Sat, 11 Jun 2005 10:34:23 +0100, Mark Broadbent wrote:
Really not sure what you are trying to say?
:)
That you are funny?
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).
The mention of "s2 = s1;"
'effectively' making a copy would lead one to think that the subsequent

's1 = "two" ; '

makes the initial object pointed to by s1 eligible for garbage collection,
whereas I should think that it is not because it is being pointed to by s2.
to put it simply, the value that is referenced by a string variable is
protected from a change from another.


I think it's just more confusing to bring up 'effective copies' to
explain that. String is immutable. The content of the object it references
cannot be changed. Period.
What's up with the new school of thought of some need for protection from
"another" ? heh.

Rico.
Nov 17 '05 #11
Not really sure why you are *still* trying to keep this thread going *but*
this is going to be my last post to it.

-See inline comments

"Rico" <ra*****@yahoo. com> wrote in message
news:pa******** *************** *****@yahoo.com ...
On Sat, 11 Jun 2005 10:34:23 +0100, Mark Broadbent wrote:
Really not sure what you are trying to say?
:)
That you are funny?

No.

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).


The mention of "s2 = s1;"
'effectively' making a copy would lead one to think that the subsequent

's1 = "two" ; '

makes the initial object pointed to by s1 eligible for garbage collection,
whereas I should think that it is not because it is being pointed to by
s2.

Yes s2 does point to this instance, examining the machine code or IL shows
this. No, my previous posts were adequate and explanatory.
Also the term "effective" is completely fine in this context. "effective" !=
"actual".

to put it simply, the value that is referenced by a string variable is
protected from a change from another.


I think it's just more confusing to bring up 'effective copies' to
explain that. String is immutable. The content of the object it references
cannot be changed. Period.

I've already explained the immutable concept.
Also your description "object it references cannot be changed" is incorrect.
The referenced location is not safe from changes by pointers.
My description *is* correct "to put it simply, the value that is referenced
by a string variable is protected from a change from another."
What's up with the new school of thought of some need for protection from
"another" ? heh.
??? Sergei's example was incorrect for this scenario, which he readily
admits.

Rico.

Nov 17 '05 #12

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

Similar topics

42
5809
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...
14
1986
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
5
3288
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
7
2221
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
5
8398
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
4
1444
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())
8
1767
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 */
7
6347
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)
2
1548
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(); }
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
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,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
jinu1996
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...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7553
isladogs
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...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
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...

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.