473,322 Members | 1,401 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,322 software developers and data experts.

Two instances

I have one object defined in a class called Class1
and these lines of code:
Dim C1 as New Class1
.... some code to define the properties of C1
Dim C2 as New Class1
C2=C1
'Then some code to define C2.

BUT the lines of code to define the properties of C2 are changing the C1's
properties as well !!

What is wrong ?

Bernard

Nov 21 '05 #1
20 1020
"Bernard Bourée" <be*****@bouree.net> schrieb:
I have one object defined in a class called Class1
and these lines of code:
Dim C1 as New Class1
... some code to define the properties of C1
Dim C2 as New Class1
C2=C1
'Then some code to define C2.

BUT the lines of code to define the properties of C2 are changing the C1's
properties as well !!

What is wrong ?


The line 'C2 = C1' will make 'C1' and 'C2' point to the same instance of
'Class1'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2

"Bernard Bourée" <be*****@bouree.net> wrote
I have one object defined in a class called Class1
and these lines of code:
Dim C1 as New Class1
... some code to define the properties of C1
Dim C2 as New Class1
C2=C1
'Then some code to define C2.

BUT the lines of code to define the properties of C2 are changing the C1's
properties as well !!

What is wrong ?

Remove this line:

C2 = C1
That line makes the association you didn't want. It makes C2 reference the
same Class1 object that C1 references (the real Class1 object is sitting
in the managed heap somewhere. That line made both variables point to
the same object)...

LFS

Nov 21 '05 #3
Thanks Herfried

But then How can I have a copy of C1 in C2 and then been able to modify it
without affecting C1 ?

Bernard

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> a écrit dans le
message de news: un**************@TK2MSFTNGP14.phx.gbl...
"Bernard Bourée" <be*****@bouree.net> schrieb:
I have one object defined in a class called Class1
and these lines of code:
Dim C1 as New Class1
... some code to define the properties of C1
Dim C2 as New Class1
C2=C1
'Then some code to define C2.

BUT the lines of code to define the properties of C2 are changing the
C1's properties as well !!

What is wrong ?


The line 'C2 = C1' will make 'C1' and 'C2' point to the same instance of
'Class1'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
"Bernard Bourée" <be*****@bouree.net> schrieb:
But then How can I have a copy of C1 in C2 and then been able to modify it
without affecting C1 ?


If 'Class1' is a reference type (in other words, not a structure) you will
have to create the copy yourself, if the class doesn't implement
'ICloneable' to provide a 'Clone' method.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5
Herfried,
If 'Class1' is a reference type (in other words, not a structure) you will
have to create the copy yourself, if the class doesn't implement
'ICloneable' to provide a 'Clone' method.

I know that you know that this is a wrong statement.

Dim a as new Herfried
Dim b as new Herfried
b = a

Than b is not the reference to the second Hefried object anymore however to
the first Herfried and the second object can be garbaged because it lost its
reference.

Larry wrote already the answer by the way.

And I am sure you know this.

:-))

Cor

Nov 21 '05 #6
"Cor Ligthert" <no************@planet.nl> schrieb:
If 'Class1' is a reference type (in other words, not a structure) you
will have to create the copy yourself, if the class doesn't implement
'ICloneable' to provide a 'Clone' method.

I know that you know that this is a wrong statement.

Dim a as new Herfried
Dim b as new Herfried
b = a

Than b is not the reference to the second Hefried object anymore however
to the first Herfried and the second object can be garbaged because it
lost its reference.

Larry wrote already the answer by the way.

And I am sure you know this.


Well, I didn't write anything different.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #7
Herfried,
Well, I didn't write anything different.

Yes you did, reread the your last message.

And when we would do it in this newsgroup exactly on the writen text, your
answer on the intermidiate question is right. However, I thought that we did
answer on what was probably the problem not on the exact English words.

It will be different when there is need for a copy of the already used
object.
However asked are "Two instances" see the subject of this message.

Cor
Nov 21 '05 #8
"Cor Ligthert" <no************@planet.nl> schrieb:
It will be different when there is need for a copy of the already used
object.
However asked are "Two instances" see the subject of this message.


Remember what the OP replied to my answer:

| But then How can I have a copy of C1 in C2 and then
| been able to modify it without affecting C1 ?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #9
Hi Herfried,
| But then How can I have a copy of C1 in C2 and then
| been able to modify it without affecting C1 ?
I get the idea you do not know how.

dim C1 as new Herfried
dim C2 as new Herfried

Or as you wish
dim C1 as Herfried
dim C2 as Herfried
C1 = new Herfried
C2 = new Herfried

C2 is now a copy of C1

I know that you know that by the way.

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> "Cor Ligthert" <no************@planet.nl> schrieb:
It will be different when there is need for a copy of the already used
object.
However asked are "Two instances" see the subject of this message.


Remember what the OP replied to my answer:

| But then How can I have a copy of C1 in C2 and then
| been able to modify it without affecting C1 ?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #10
"Cor Ligthert" <no************@planet.nl> schrieb:
| But then How can I have a copy of C1 in C2 and then
| been able to modify it without affecting C1 ?


I get the idea you do not know how.

dim C1 as new Herfried
dim C2 as new Herfried

Or as you wish
dim C1 as Herfried
dim C2 as Herfried
C1 = new Herfried
C2 = new Herfried

C2 is now a copy of C1


Why is 'C2' a "copy" of 'C1'? Are you talking about the
variables/references or objects the variables are pointing to?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #11
Herfried,

When you go tomorrow to the univesity go to a copier, lay a piece of paper
on the glas.
Push on "2" copies and than you will see it.

Otherwise look in the german dictionary what a "kopie" is.

It is not the same paper and yet it are copies because the contents is
equal.

Cor
"Herfried K. Wagner [MVP]" <hi***************@gmx.at>
"Cor Ligthert" <no************@planet.nl> schrieb:
| But then How can I have a copy of C1 in C2 and then
| been able to modify it without affecting C1 ?


I get the idea you do not know how.

dim C1 as new Herfried
dim C2 as new Herfried

Or as you wish
dim C1 as Herfried
dim C2 as Herfried
C1 = new Herfried
C2 = new Herfried

C2 is now a copy of C1


Why is 'C2' a "copy" of 'C1'? Are you talking about the
variables/references or objects the variables are pointing to?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #12
"Cor Ligthert" <no************@planet.nl> schrieb:
When you go tomorrow to the univesity go to a copier, lay a piece of paper
on the glas.
Push on "2" copies and than you will see it.

Otherwise look in the german dictionary what a "kopie" is.

It is not the same paper and yet it are copies because the contents is
equal.


LOL -- I know that... But what's the /copy/ in your sample?!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #13
Ouf!!
I must admit that I haven't completly understood this thread,
but my problem is still not solved.
My question was : how can I have a copy of an object (class defined) with
all its properties and been able to modify it without changing the original
one.
if C2=C1 is not correct since it makes both objevt pointing to the same
address, which code should I use ?

Bernard
"Larry Serflaten" <se*******@usinternet.com> a écrit dans le message de
news: %2****************@TK2MSFTNGP12.phx.gbl...

"Bernard Bourée" <be*****@bouree.net> wrote
I have one object defined in a class called Class1
and these lines of code:
Dim C1 as New Class1
... some code to define the properties of C1
Dim C2 as New Class1
C2=C1
'Then some code to define C2.

BUT the lines of code to define the properties of C2 are changing the
C1's
properties as well !!

What is wrong ?

Remove this line:

C2 = C1
That line makes the association you didn't want. It makes C2 reference
the
same Class1 object that C1 references (the real Class1 object is sitting
in the managed heap somewhere. That line made both variables point to
the same object)...

LFS

Nov 21 '05 #14
"Bernard Bourée" <be*****@bouree.net> schrieb:
I must admit that I haven't completly understood this thread,
but my problem is still not solved.
My question was : how can I have a copy of an object (class defined) with
all its properties and been able to modify it without changing the
original one.
if C2=C1 is not correct since it makes both objevt pointing to the same
address, which code should I use ?


\\\
Dim C1 As New Class1()
Dim C2 As Class1 = C1.MemberwiseClone()
///

.... if 'Class1' implements the 'ICloneable' interface:

\\\
Dim C1 As New Class1()
Dim C2 As Class2 = C1.Clone()
///

Take a look at 'ICloneable.Clone' and 'Object.MemberwiseClone' in the
documentation. For easier cloning, you can make 'Class1' a value type (if
it is "small"):

\\\
Public Structure Class1
Public ID As Integer
Public Cost As Decimal
End Structure
..
..
..
Dim C1 As Class1 ' Implicitly 'As New Class1'!
Dim C2 As Class1 = C1
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #15
Bernard,

If you want to create a new object from an existing object implement the
ICloneable interface in the object's class statement. Here is a class that
implements the ICloneable interface:

Public Class Insured
Implements ICloneable

Public Company As String
Public Name As String

Public Function Clone() as Object Implements ICloneable.Clone
' Create a new Insured object and set its property values equal to
those of the object instance upon
' which this method is called.
Dim insuredClone As New Insured()
insuredClone.Company = me.Company
insuredClone.Name = me.Name
Return insuredClone
End Function

End Class

-----------

' Example use.

Dim C1 As New Insured()
C1.Company = "Hartford"
' At this point one Insured object has been created in memory and C1
references (points to) it. The Company property of the object is "Hartford"
and its Name property is "" (empty).

Dim C2 As Insured = C1.Clone()

' At this point two Insured objects have been created in memory. The second
Insured method was created by calling the first object's Clone method and
assigning the resulting Insured object to the C2 variable. The first and
second object's Company property is "Hartford. The first and second
object's Name property is "" (empty)

C1.Name = "Tim"
C2.Name = "Bob"

' At this point C1 references the first Insured object created. Its Company
property is "Hartford"; its Name property is "Tim".
' C2 references the second Insured object created. Its Company property is
"Hartford"; its Name property is "Bob".
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
"Bernard Bourée" <be*****@bouree.net> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
I have one object defined in a class called Class1
and these lines of code:
Dim C1 as New Class1
... some code to define the properties of C1
Dim C2 as New Class1
C2=C1
'Then some code to define C2.

BUT the lines of code to define the properties of C2 are changing the C1's
properties as well !!

What is wrong ?

Bernard

Nov 21 '05 #16

"Bernard Bourée" <be*****@bouree.net> wrote
Ouf!!
I must admit that I haven't completly understood this thread,
but my problem is still not solved.
My question was : how can I have a copy of an object (class defined) with
all its properties and been able to modify it without changing the original
one.
if C2=C1 is not correct since it makes both objevt pointing to the same
address, which code should I use ?


You need to add a function to the class that will return a copy of the object.
The name for this type of operation is often called Clone, and to insure others
can 'discover' if your object supports the Clone method, you can implement
the common interface called ICloneable, as Herfried and Mike McIntyre suggested.

LFS

Nov 21 '05 #17
If you make an object serializable, you can use the following routine to make
a "deepcopy" instead of the shallow copy you are now making:

Private Sub DeepCopy(ByRef src As Object, ByRef dest As Object)
'Makes Deep Copy of src to dest
'Note both objects must be of same type and serialzable
Static ms As MemoryStream = New MemoryStream
Static fmtr As New BinaryFormatter
ms.Seek(0, SeekOrigin.Begin)
fmtr.Serialize(ms, src)
ms.Seek(0, SeekOrigin.Begin)
dest = fmtr.Deserialize(ms)
End Sub

"Bernard Bourée" wrote:
I have one object defined in a class called Class1
and these lines of code:
Dim C1 as New Class1
.... some code to define the properties of C1
Dim C2 as New Class1
C2=C1
'Then some code to define C2.

BUT the lines of code to define the properties of C2 are changing the C1's
properties as well !!

What is wrong ?

Bernard

Nov 21 '05 #18
Doesn't that only work if all fields of the structure are value type. If one
is a reference type, won't it be just a pointer to the same object as the
copy?

"Herfried K. Wagner [MVP]" wrote:
"Bernard Bourée" <be*****@bouree.net> schrieb:
I must admit that I haven't completly understood this thread,
but my problem is still not solved.
My question was : how can I have a copy of an object (class defined) with
all its properties and been able to modify it without changing the
original one.
if C2=C1 is not correct since it makes both objevt pointing to the same
address, which code should I use ?


\\\
Dim C1 As New Class1()
Dim C2 As Class1 = C1.MemberwiseClone()
///

.... if 'Class1' implements the 'ICloneable' interface:

\\\
Dim C1 As New Class1()
Dim C2 As Class2 = C1.Clone()
///

Take a look at 'ICloneable.Clone' and 'Object.MemberwiseClone' in the
documentation. For easier cloning, you can make 'Class1' a value type (if
it is "small"):

\\\
Public Structure Class1
Public ID As Integer
Public Cost As Decimal
End Structure
..
..
..
Dim C1 As Class1 ' Implicitly 'As New Class1'!
Dim C2 As Class1 = C1
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #19
Thanks to all for your help
Problem solved.

Bernard
"Dennis" <De****@discussions.microsoft.com> a écrit dans le message de news:
23**********************************@microsoft.com...
If you make an object serializable, you can use the following routine to
make
a "deepcopy" instead of the shallow copy you are now making:

Private Sub DeepCopy(ByRef src As Object, ByRef dest As Object)
'Makes Deep Copy of src to dest
'Note both objects must be of same type and serialzable
Static ms As MemoryStream = New MemoryStream
Static fmtr As New BinaryFormatter
ms.Seek(0, SeekOrigin.Begin)
fmtr.Serialize(ms, src)
ms.Seek(0, SeekOrigin.Begin)
dest = fmtr.Deserialize(ms)
End Sub

"Bernard Bourée" wrote:
I have one object defined in a class called Class1
and these lines of code:
Dim C1 as New Class1
.... some code to define the properties of C1
Dim C2 as New Class1
C2=C1
'Then some code to define C2.

BUT the lines of code to define the properties of C2 are changing the
C1's
properties as well !!

What is wrong ?

Bernard

Nov 21 '05 #20
"Dennis" <De****@discussions.microsoft.com> schrieb:
Doesn't that only work if all fields of the structure are value type. If
one
is a reference type, won't it be just a pointer to the same object as the
copy?


Yes, that's true. What you want to do is a deep copy of the object.
Nevertheless, creating deep copies is non-trivial if there are circular
references.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #21

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

Similar topics

22
by: christof hoeke | last post by:
hello, this must have come up before, so i am already sorry for asking but a quick googling did not give me any answer. i have a list from which i want a simpler list without the duplicates an...
4
by: Brad Tilley | last post by:
I've just started using classes in Python for some projects at work and have a few questions about them. I understand that once a class is defined that I can create many instances of it like this:...
8
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
4
by: Yasaswi Pulavarti | last post by:
I have a Aix 5.2 server with DB2 UDB 8.1 I created three different instances using the db2setup utility three times. The default instance is db2inst1. When I su - db2inst2 and su - db2inst3 and...
12
by: (Pete Cresswell) | last post by:
I know I can open many instances of a given form, but I've never done it. Now I'm analyzing an application where that seems like just the ticket: Many investment funds, *lots* of data points for...
4
by: Chad Myers | last post by:
I'm instrumenting my app with a few performance counters and I'd like to ask you all for some advice on how to handle performance counter instances. I have a class library that is a base library...
3
by: Boni | last post by:
Dear all, I create a big number of a class instances of some class. Sometimes this instances must be collected (all pointers are deleted, but instance is not explicitly disposed).But sometimes I...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
4
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
5
by: Neil | last post by:
"lyle" <lyle.fairfield@gmail.comwrote in message news:48c3dde7-07bd-48b8-91c3-e157b703f92b@f3g2000hsg.googlegroups.com... Question for you. I'm doing something similar, only, instead of opening...
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: 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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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

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.