-2

I'm currently working on a program to implement Dijkstra's algorithm just for fun really. Its probably far beyond my skill level but I thought I'd give it a go anyway. So far I've made a vertex class which - among other things - has a property called Connections which is a list of Other Vertices it is connected to. When I use my Form to add a new Vertex to my Graph(List of Vertices), it doesn't seem to keep and remember the connections I have assigned it. I've tried debugging and it just seems to mysteriously disappear after it has been assigned in the class. Any Help would be greatly appreciated.

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

I create a new Vertex on Button1.click which should be added to the "Graph" list. The vertex is added but does not have any connections when I try to repopulate ListBox3

1 Answers1

0

You seem to be under the impression that this line:

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

is going to duplicate the List(Of Vertex) object assigned to tempConnections. It is not. There is just one List(Of Vertex) so when you later do this:

tempConnections.Clear()

you are clearing that one and only List(Of Vertex) that your new Vertex referred to.

Think about this. Let's say that you and I are going to share an apartment. You are already living there and have filled the cutlery draw with all your knives and forks. When I move in, that cutlery draw is now my cutlery draw too, as well as yours. Does that mean that there are magically now two cutlery draws? No, of course not. There's just one cutlery draw that we both use. Now, if I clear out my cutlery, what would you expect to see if you opened your cutlery draw? Your cutlery draw would be empty, right?

This is exactly the same situation. Object-oriented programming is called that for a reason. Programming objects are supposed to behave just like real-life objects and they are in your program. There's just one List(Of Vertex) and you're referring to it from multiple places. If you clear it in one of those places then every other place will see an empty list.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46