gpt4 book ai didi

vba - 将变量设置为列表中的下一个实例不起作用

转载 作者:行者123 更新时间:2023-12-02 19:16:57 24 4
gpt4 key购买 nike

上下文:我正在编写 Dijkstra 的最短路径算法,只是为了练习我在 教程中读到的基本模式。 .

我遇到了一个有问题的情况。经过长时间调试,我发现了问题。

这是显示问题的缩小版本(已注释):

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

我的model_linked_list只是:

Option Explicit

Public city As Integer
Public next_cell As model_linked_list

举例来说,上面的代码应该只创建一个如下所示的列表:

enter image description here

当我尝试转到列表中的下一个单元格时,Set 似乎不起作用。有人见过这个吗?如何解决这个问题?

提前致谢。

最佳答案

你的错误在这里:

Dim cell As New model_linked_list

改成:

Dim cell As model_linked_list
Set cell = New model_linked_list

简而言之,使用 Dim cell As New model_linked_list 时,“New” 运算符仅执行一次,因此您在所有迭代中都在操作同一对象。

关于vba - 将变量设置为列表中的下一个实例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45022616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com