3

Context: I'm coding the Dijkstra's Shortest Path Algorithm just to practice basic patterns I've read on tutorials about .

I ran into a problematic situation. After long time debugging I found the issue.

Here is a minified version which shows the problem (commented):

Option Explicit

Private Sub btnSolution_Click()
    Dim N As Integer: N = 3

    'Creating one list with the first cell as head
    Dim list As New model_linked_list
    Set list.next_cell = Nothing

    'Creates list 1..N
    Dim i As Integer: i = N
    Do While i > 0
        'Creates the next cell adding to the start (after the head-cell)
        Dim cell As New model_linked_list
        cell.city = i
        Set cell.next_cell = list.next_cell

        'Update head-cell
        Set list.next_cell = cell
        i = i - 1
    Loop

    'All good till here, but when I try to loop over the list:

    Dim item As model_linked_list
    Set item = list.next_cell

    'You will to set a breakpoint in this line to avoid infinite loop
    Do While Not item Is Nothing

        MsgBox item.city 'Always shows "1"

        'This is the problematic line
        Set item = item.next_cell 'It seems like it does nothing, literally
    Loop

End Sub

enter image description here

My model_linked_list is just:

Option Explicit

Public city As Integer
Public next_cell As model_linked_list

Illustrating, the above code should just create one list like this:

enter image description here

It seems like Set is just not working when I try to go to the next cell in the list. Have anyone seen this before? How to work around it?

Thanks in advance.

Community
  • 1
  • 1
Washington Guedes
  • 4,254
  • 3
  • 30
  • 56
  • 2
    You should create a `New` copy of `Cell` each time you loop through the first loop, so `Set Cell = New model_linked_list`. At the moment you are just updating the one instance. (Remember that `Dim` statements are only done at the start of the subroutine, no matter where they are located in the code.) – YowE3K Jul 10 '17 at 22:50
  • Possible duplicate of [VBA: Difference in two ways of declaring a new object? (Trying to understand why my solution works)](https://stackoverflow.com/questions/2478097/vba-difference-in-two-ways-of-declaring-a-new-object-trying-to-understand-why) – YowE3K Jul 10 '17 at 22:57

1 Answers1

2

Your mistake is here:

Dim cell As New model_linked_list

change it into:

Dim cell As model_linked_list
Set cell = New model_linked_list

In short, with Dim cell As New model_linked_list, the "New" operator was executed only once, so you were manipulating the same object in all your iterations.

A.S.H
  • 29,101
  • 5
  • 23
  • 50
  • My comment beat your answer by about 20 seconds (but I didn't have to make my comment look pretty) :D – YowE3K Jul 10 '17 at 22:51
  • @YowE3K Dóh, why do you hate writing answers?? :D In fact I was considering writing just a correction but then thought it is worth adding some explanation. I feel OP comes from some C++ background. – A.S.H Jul 10 '17 at 22:53
  • Because I had this vague feeling that the question would be marked as a duplicate anyway - I know there have been lots of times where I have seen people being told not to use the `Dim x As New y` structure. (If I could just **remember** one of those cases, I would mark it as duplicate myself! :D P.S. I just googled `site:stackoverflow.com excel-vba dim as new` and found the appropriate dup.) – YowE3K Jul 10 '17 at 22:55
  • @YowE3K Instead of googling I was following the code and testing it. OP provided a nice MCVE and the question has its own flavor even if at the end the same error that we found occured in another thread. `Dijkstra` (particularly attractive to me), linked list, class etc... :) – A.S.H Jul 10 '17 at 23:04
  • 1
    Thank you guys... I learned something new with it, and my code is working perfectly now :) – Washington Guedes Jul 10 '17 at 23:04
  • 1
    I actually upvoted the question, even though it is a dup - it does provide a good landing point for people searching for this issue. – YowE3K Jul 10 '17 at 23:05