VB.net 同一类的单独列表相互复制
这个例子显示了我的问题。我正在使用 VB.net 2010
Public Class Form1
Public Class BonoType
Public name As String
End Class
Private tory As New List(Of BonoType)
Private tory1 As New List(Of BonoType)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim gg As New BonoType
gg.name = "Boopsy"
tory.Add(gg)
gg = New BonoType
gg.name = "Dipsy"
tory.Add(gg)
tory1 = tory
Label1.Text = tory1(0).name
Label2.Text = tory1(1).name
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
tory(1).name = "Goose"
Label1.Text = tory1(0).name
Label2.Text = tory1(1).name
TextBox1.Text = tory(1).name
End Sub
End Class
发生的情况是“Goose”不仅存储在 tory(1) 中,而且还存储在 tory1(1) 中,我该如何阻止这种情况。
this example shows my problem. I'm using VB.net 2010
Public Class Form1
Public Class BonoType
Public name As String
End Class
Private tory As New List(Of BonoType)
Private tory1 As New List(Of BonoType)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim gg As New BonoType
gg.name = "Boopsy"
tory.Add(gg)
gg = New BonoType
gg.name = "Dipsy"
tory.Add(gg)
tory1 = tory
Label1.Text = tory1(0).name
Label2.Text = tory1(1).name
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
tory(1).name = "Goose"
Label1.Text = tory1(0).name
Label2.Text = tory1(1).name
TextBox1.Text = tory(1).name
End Sub
End Class
What happens is "Goose" is not only stored in tory(1) but also in tory1(1), how can I stop this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信你的问题就在这里,
你将 tory1 设置为 tory。遵循你想要完成的事情有点困难,但在我看来,它们实际上是捆绑在一起的。您可能需要考虑
i believe your problem lies here
you are setting tory1 equal to tory. Its kind of hard to follow what you are trying to acomplish but it would appear to me that they are actually binded together. You might want to consider
我认为问题在于,使用 tory1=tory,您只是创建了对原始对象的引用。您需要创建一个新对象。
即,在 VB 中:
然后,当您要复制 tory2:
c# 时:
I think the problem is that with tory1=tory, you're just creating a reference back to the original object. You need to create a new object instead.
I.e., in VB:
Then, when you want to copy tory2:
c#: