MyClass2 = MyClass1 - > ????为ByRef [英] MyClass2 = MyClass1 -> ByRef????

查看:45
本文介绍了MyClass2 = MyClass1 - > ????为ByRef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有2个用户定义类的实例。当我执行cls2 = cls1,并且我更改了cls2的属性时,我的cls1中的属性也发生了变化!!


有谁知道是什么这是为什么?以及如何阻止它?


谢谢,


彼得


我是使用VB.NET 2003.

这是我的示例代码:


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click

Dim cls1 As new clsMyClass

Dim cls2 as clsMyClass

cls2 = Nothing

cls2 = cls1

MessageBox.Show(cls1.Icon什么都没有)

Dim bmp As New Bitmap(20,20,Imaging.PixelFormat.Format32bppPArgb )

cls2.Icon = bmp

MessageBox.Show(cls1.Icon什么都没有)

End Sub


公共类clsMyClass

私有m_Icon作为位图


公共属性图标()作为位图

获取

返回m_Icon

结束获取

设置(ByVal值为位图)

m_Icon =价值

结束集

结束财产


结束班

解决方案



有很多方法可以做到这一点,但是防止它的一种方法

将使用这样的复制构造函数: (这应该可以做到这一点)


公共类clsMyClass

私有m_Icon作为位图


Public Sub New( )

''暂时不做任何事,但你可以添加你的代码

End Sub


Public Sub New( ByVal ClsToCopy as clsMyClass)

Me.Icon = ClsToCopy.Icon

End Sub


Public Property Icon()As Bitmap

获取

返回m_Icon

结束获取

设置(ByVal值为位图)

m_Icon =价值

结束套件

结束物业


结束班级


" DraguVaso" < PI ********** @ hotmail.com> écritdansle message de

news:呃************* @ TK2MSFTNGP10.phx.gbl ...



我有2个用户定义类的实例。当我执行cls2 = cls1,并且我更改了cls2的属性时,我的cls1中的属性也发生了变化!!

有谁知道这是什么原因?以及如何防止它?

谢谢,

Pieter

我正在使用VB.NET 2003.

这是我的示例代码:

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
Dim cls1 As New clsMyClass
Dim cls2 as clsMyClass
cls2 = Nothing
cls2 = cls1
MessageBox.Show(cls1.Icon什么都没有)
Dim bmp As New Bitmap(20,20) ,
Imaging.PixelFormat.Format32bppPArgb)cls2.Icon = bmp
MessageBox.Show(cls1.Icon什么都没有)
End Sub

Public Class clsMyClass
私有m_Icon作为位图

公共属性图标()作为位图
获取
返回m_Icon
结束获取
设置(ByVal值为位图)
m_Icon =价值
结束集
结束属性

结束类



那是因为cls1和cls2是对象的*引用*而不是

实际对象本身。将cls2分配给cls1简单地使它们

都引用相同的对象,这就是为什么更改属性

会影响两者。


我建议您在用户定义的

类中实现ICloneable接口,并执行类似

cls2 =(MyClass)cls1.Clone();


问候

Senthil


DraguVaso写道:

我有用户定义的类的2个实例。当我执行cls2 = cls1,
并更改cls2的属性时,此属性也在我的
cls1中更改!!




你没有两个实例,你只有一个。


你的实例是在这一行创建的:


\\ \

Dim cls1 As new clsMyClass

///


以下行不会创建另一个实例,它只是指向在cls1已经引用的现有实例中,cls2




\\\

cls2 = cls1

///


此后,cls1和cls2都指向同一个对象实例,因此你所描述的行为就是



为了防止它,创建两个对象实例:

\\\

Dim cls1 As新clsMyClass

Dim cls2 As new clsMyClass

///


-


(O)enone


Hi,

I have 2 instances of a user-defined class. When I do a cls2 = cls1, and I
change a property of cls2, this property is also changed in my cls1!!

Does anybody know what the reason of this is? and how I can prevent it?

Thanks,

Pieter

I''m using VB.NET 2003.
This is my sample code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cls1 As New clsMyClass
Dim cls2 As clsMyClass
cls2 = Nothing
cls2 = cls1
MessageBox.Show(cls1.Icon Is Nothing)
Dim bmp As New Bitmap(20, 20, Imaging.PixelFormat.Format32bppPArgb)
cls2.Icon = bmp
MessageBox.Show(cls1.Icon Is Nothing)
End Sub

Public Class clsMyClass
Private m_Icon As Bitmap

Public Property Icon() As Bitmap
Get
Return m_Icon
End Get
Set(ByVal Value As Bitmap)
m_Icon = Value
End Set
End Property

End Class

解决方案

Hi,
There are probably many ways to accomplish this, but one way to prevent it
would be to use a copy constructor like this: (That should do the trick)

Public Class clsMyClass
Private m_Icon As Bitmap

Public Sub New()
''Do nothing for now, but you can add your code to it
End Sub

Public Sub New(ByVal ClsToCopy as clsMyClass)
Me.Icon = ClsToCopy.Icon
End Sub

Public Property Icon() As Bitmap
Get
Return m_Icon
End Get
Set(ByVal Value As Bitmap)
m_Icon = Value
End Set
End Property

End Class

"DraguVaso" <pi**********@hotmail.com> a écrit dans le message de
news:uh*************@TK2MSFTNGP10.phx.gbl...

Hi,

I have 2 instances of a user-defined class. When I do a cls2 = cls1, and I
change a property of cls2, this property is also changed in my cls1!!

Does anybody know what the reason of this is? and how I can prevent it?

Thanks,

Pieter

I''m using VB.NET 2003.
This is my sample code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cls1 As New clsMyClass
Dim cls2 As clsMyClass
cls2 = Nothing
cls2 = cls1
MessageBox.Show(cls1.Icon Is Nothing)
Dim bmp As New Bitmap(20, 20, Imaging.PixelFormat.Format32bppPArgb) cls2.Icon = bmp
MessageBox.Show(cls1.Icon Is Nothing)
End Sub

Public Class clsMyClass
Private m_Icon As Bitmap

Public Property Icon() As Bitmap
Get
Return m_Icon
End Get
Set(ByVal Value As Bitmap)
m_Icon = Value
End Set
End Property

End Class



That''s because cls1 and cls2 are *references* to objects and not the
actual objects themselves. Assigning cls2 to cls1 simply makes them
both refer to the same object and that''s why changing a property
affects both.

I suggest you implement the ICloneable interface in your user defined
class and do something like
cls2 = (MyClass)cls1.Clone();

Regards
Senthil


DraguVaso wrote:

I have 2 instances of a user-defined class. When I do a cls2 = cls1,
and I change a property of cls2, this property is also changed in my
cls1!!



You don''t have two instances, you only have one.

Your instance is created in this line:

\\\
Dim cls1 As New clsMyClass
///

The following line does not create another instance, it simply points cls2
at the existing instance that cls1 is already referencing:

\\\
cls2 = cls1
///

After this, both cls1 and cls2 are pointing to the same object instance,
hence the behaviour you described.

To prevent it, create two object instances:

\\\
Dim cls1 As New clsMyClass
Dim cls2 As New clsMyClass
///

--

(O)enone


这篇关于MyClass2 = MyClass1 - &gt; ????为ByRef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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