两个例子 [英] Two instances

查看:71
本文介绍了两个例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为Class1

的类中定义了一个对象,这些代码行:

Dim C1为New Class1

.. ..一些代码定义C1的属性

Dim C2为新Class1

C2 = C1

''然后一些代码来定义C2 。


但是用于定义C2属性的代码行也正在改变C1的

属性!!


有什么问题?


Bernard

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

推荐答案

" BernardBourée <是***** @ bouree.net> schrieb:
"Bernard Bourée" <be*****@bouree.net> schrieb:
我在一个名为Class1的类中定义了一个对象
这些代码行:
Dim C1 as New Class1
...定义一些代码C1的属性
Dim C2为新Class1
C2 = C1
''然后用一些代码来定义C2。

但代码行定义属性C2正在改变C1'的属性!!

有什么问题?
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 ?




这行'' C2 = C1''将''C1''和''C2''指向同一个实例

''Class1''。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< ;网址:http://dotnet.mvps.org/dotnet/faqs/>



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/>




"BernardBourée" ; <是***** @ bouree.net>写了

"Bernard Bourée" <be*****@bouree.net> wrote
我在一个名为Class1的类中定义了一个对象
这些代码行:
Dim C1 as New Class1
...一些代码来定义C1的属性
Dim C2为新Class1
C2 = C1
''然后用一些代码来定义C2。

但代码行定义属性C2正在改变C1的属性!!

有什么问题?
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 ?



删除此行:


C2 = C1

该行使您不想要的关联。它使C2引用了

与C1引用相同的Class1对象(真正的Class1对象在某个地方的托管堆中位于
。该行使两个变量都指向

同一个对象)...


LFS


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


谢谢Herfried


但是那我怎样才能在C2中获得C1的副本然后能够修改它

而不影响C1?


Bernard


" Herfried K. Wagner [MVP]" <喜*************** @ gmx.at> aécritdansle

message de news: un *** ***********@TK2MSFTNGP14.phx.gbl ...
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...
"BernardBourée" <是***** @ bouree.net> schrieb:
"Bernard Bourée" <be*****@bouree.net> schrieb:
我在一个名为Class1的类中定义了一个对象
这些代码行:
Dim C1 as New Class1
...定义一些代码C1的属性
Dim C2为新Class1
C2 = C1
''然后用一些代码来定义C2。

但代码行定义属性C2正在改变
C1'的属性!!

有什么问题?
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 ?



行''C2 = C1' '将使''C1''和''C2''指向同一个实例''Class1''。

-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://dotnet.mvps.org/dotnet/faqs/>



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/>



这篇关于两个例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆