【发布时间】: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 查看您问题的源代码,了解如何添加格式化代码。
-
非常感谢为我编辑的人。