Two instances

  • Thread starter Thread starter Bernard Bourée
  • Start date Start date
B

Bernard Bourée

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
 
Bernard Bourée said:
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'.
 
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
 
Bernard Bourée said:
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
 
Bernard Bourée said:
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.
 
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
 
Cor Ligthert said:
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.
 
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
 
Cor Ligthert said:
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 ?
 
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
 
Cor Ligthert said:
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?
 
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
 
Cor Ligthert said:
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?!
 
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
 
Bernard Bourée said:
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
///
 
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 said:
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
 
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
 
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top