【问题标题】:List Mysteriously Deletes Itself With Seemingly No Explanation列表神秘地删除了自己,似乎没有任何解释
【发布时间】:2014-09-29 19:15:30
【问题描述】:

我目前正在开发一个程序来实现 Dijkstra 算法,这真的只是为了好玩。它可能远远超出了我的技能水平,但我想我还是会试一试。 到目前为止,我已经创建了一个顶点类,其中包括一个名为 Connections 的属性,它是它所连接的其他顶点的列表。当我使用我的表单向我的图表(顶点列表)添加一个新顶点时,它似乎没有保留并记住我分配给它的连接。我试过调试,它似乎在课堂上分配后神秘地消失了。任何帮助将不胜感激。

Form1.vb

Public Class Form1
    Dim Graph As New List(Of Vertex)
    Dim vertS As New Vertex("S", New List(Of Vertex)(), New List(Of Double)(), True, 0)
    Dim vertT As New Vertex("T", New List(Of Vertex)(), New List(Of Double)(), False)
    Dim tempLengths As New List(Of Double)
    Dim tempConnections As New List(Of Vertex)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Graph.Add(vertT)
        Graph.Add(vertS)
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        For Each vert In Graph
            ListBox1.Items.Add(vert.Key)
            ListBox2.Items.Add(vert.Key)
        Next
    End Sub

    Sub RefreshAll()
        TextBox1.Text = ""
        tempConnections.Clear()
        tempLengths.Clear()
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        ListBox3.Items.Clear()
        For Each vert In Graph
            ListBox1.Items.Add(vert.Key)
            ListBox2.Items.Add(vert.Key)
        Next
    End Sub
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim chosenVert = Graph(ListBox1.SelectedIndex)
        TextBox1.Text = chosenVert.Key
        TextBox2.Text = ""
        ListBox3.Items.Clear()
        For Each con In chosenVert.Connections
            ListBox3.Items.Add(con.Key)
        Next
    End Sub

    Private Sub btnAddCon_Click(sender As Object, e As EventArgs) Handles btnAddCon.Click
        ListBox3.Items.Add(ListBox2.SelectedItem)
        tempConnections.Add(Graph(ListBox2.SelectedIndex))
        tempLengths.Add(Val(TextBox2.Text))
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Graph.Add(New Vertex(TextBox1.Text, tempConnections, tempLengths, False))
        RefreshAll()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Graph(ListBox1.SelectedIndex).Connections.AddRange(tempConnections)
        Graph(ListBox1.SelectedIndex).Lengths.AddRange(tempLengths)
        Graph(ListBox1.SelectedIndex).Key = TextBox1.Text
        RefreshAll()
    End Sub

End Class

Vertex.vb

Public Class Vertex
    Private pCompValue As Double
    Private pCompleted As Boolean
    Private pConnections As New List(Of Vertex)
    Private pLengths As New List(Of Double)
    Private pKey As String
    Property CompValue() As Double
        Get
            Return pCompValue
        End Get
        Set(value As Double)
            pCompValue = value
        End Set
    End Property

    Property Completed As Boolean
        Get
            Return pCompleted
        End Get
        Set(value As Boolean)
            pCompleted = value
        End Set
    End Property

    Property Connections As List(Of Vertex)
        Get
            Return pConnections
        End Get
        Set(value As List(Of Vertex))
            pConnections = value
        End Set
    End Property

    Property Lengths As List(Of Double)
        Get
            Return pLengths
        End Get
        Set(value As List(Of Double))
            pLengths = value
        End Set
    End Property

    Property Key As String
        Get
            Return pKey
        End Get
        Set(value As String)
            pKey = value
        End Set
    End Property

    Sub New(ByVal pKey As String, ByVal pConnections As List(Of Vertex), ByVal pLengths As List(Of Double), ByVal pCompleted As Boolean, Optional ByVal pCompValue As Double = 10000000000)
        Key = pKey
        Connections = pConnections
        Lengths = pLengths
        Completed = pCompleted
        CompValue = pCompValue
    End Sub



End Class

我在 Button1.click 上创建了一个新顶点,它应该被添加到“图表”列表中。当我尝试重新填充 ListBox3 时,顶点已添加但没有任何连接

【问题讨论】:

  • 学习在这里粘贴和格式化代码,不要发布代码链接(外部网站可能不可用...)
  • 我已从您的 pastebin 添加了代码 - 请点击 edit 查看您问题的源代码,了解如何添加格式化代码。
  • 非常感谢为我编辑的人。

标签: vb.net list class vertex


【解决方案1】:

您似乎认为这行:

Graph.Add(New Vertex(TextBox1.Text, tempConnections, tempLengths, False))

将复制分配给tempConnections 的List(Of Vertex) 对象。它不是。只有一个List(Of Vertex),所以当您稍后执行此操作时:

tempConnections.Clear()

您正在清除您的新 Vertex 引用的唯一一个 List(Of Vertex)。

考虑一下。假设你和我要合住一间公寓。你已经住在那里,并且已经用你所有的刀叉装满了餐具。当我搬进来时,那个餐具抽奖现在也是我的餐具抽奖,以及你的。这是否意味着现在神奇地有两个餐具抽奖?不,当然不。我们都使用的只有一个餐具抽签。现在,如果我清理我的餐具,如果你打开你的餐具抽签,你会看到什么?你的餐具抽奖是空的,对吧?

这是完全一样的情况。面向对象编程被称为是有原因的。编程对象应该像现实生活中的对象一样表现,并且它们在您的程序中。只有一个List(Of Vertex),您从多个地方引用它。如果您在其中一个地方清除它,那么其他所有地方都会看到一个空列表。

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 2011-06-16
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多