VB.net 同一类的单独列表相互复制

发布于 2025-01-08 20:17:44 字数 1079 浏览 0 评论 0原文

这个例子显示了我的问题。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国产ˉ祖宗 2025-01-15 20:17:44

我相信你的问题就在这里,

   tory1 = tory

你将 tory1 设置为 tory。遵循你想要完成的事情有点困难,但在我看来,它们实际上是捆绑在一起的。您可能需要考虑

 var thisTory = new tory
 Label1.Text = thisTory(0).name
 Label2.Text = tory1(1).name

i believe your problem lies here

   tory1 = tory

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

 var thisTory = new tory
 Label1.Text = thisTory(0).name
 Label2.Text = tory1(1).name
杯别 2025-01-15 20:17:44

我认为问题在于,使用 tory1=tory,您只是创建了对原始对象的引用。您需要创建一个新对象。

即,在 VB 中:

Dim tory as New List(of String);
Dim tory2 as List(of String)

然后,当您要复制 tory2:

tory2 = New List(of String)(tory)

c# 时:

List<String> tory = new List<String>();
List<String> tory2;

tory2 = new List<String>(tory);

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:

Dim tory as New List(of String);
Dim tory2 as List(of String)

Then, when you want to copy tory2:

tory2 = New List(of String)(tory)

c#:

List<String> tory = new List<String>();
List<String> tory2;

tory2 = new List<String>(tory);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文