列表神秘地删除自己看似没有解释

时间:2014-09-29 19:15:31

标签: vb.net list class vertex

我目前正致力于实施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上创建了一个新的顶点,它应该添加到" Graph"名单。当我尝试重新填充ListBox3时,顶点被添加但没有任何连接

1 个答案:

答案 0 :(得分:0)

你似乎认为这一行:

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

将复制分配给List(Of Vertex)的{​​{1}}对象。它不是。只有一个tempConnections所以当你以后这样做时:

List(Of Vertex)

您正在清除新tempConnections.Clear() 所指的那个List(Of Vertex)

想一想。让我们说你和我要共用一套公寓。你已经住在那里,用刀具和叉子填满了餐具。当我搬进去的时候,那个餐具画画现在也是我的餐具画画,也是你的。这是否意味着现在神奇地有两个餐具画?不,当然不。我们都使用了一个餐具画。现在,如果我清理掉餐具,如果你打开餐具画,你会期待看到什么?你的餐具抽奖是空的,对吗?

这是完全相同的情况。出于某种原因,调用面向对象的编程。编程对象应该像现实生活中的对象一样运行,并且它们在您的程序中。只有一个Vertex,你从多个地方引用它。如果你在其中一个地方清除它,那么每个其他地方都会看到一个空列表。