如何在sub中创建一个新对象并将其分配给一个参数?

时间:2013-07-20 16:11:10

标签: excel class vba

我在vba中有以下代码,excel。首先,我创建了类'List':

Public next as List
Public val as Integer

接下来我想做这样的事情

Sub Test()
    Dim ls as List
    Set ls = new List

     Set_val ls.next, 8
     Debug.Print (ls.next is Nothing) 'True - why?
     Debug.Print ls.next.val 'Error
end sub


Sub Set_val(l as List, v as Integer)
     Set l = new List
     l.val = v
end sub

但它不起作用,我收到错误消息“没有设置块变量的对象或变量”。你知道怎么做得好吗?

2 个答案:

答案 0 :(得分:1)

请改为尝试:

Sub Test()
    Dim ls as List
    Set ls = new List

     Set ls.Next = Set_val(8)
     Debug.Print (ls.next is Nothing) 'True - why?
     Debug.Print ls.next.val 'Error
end sub

Function Set_val(v as Integer) as List
    Set Set_val = new List
    Set_val.val = v
end sub

答案 1 :(得分:0)

Public next As List无法使用,因为Next无法使用。

您永远不会更改next的值。该函数改为更改val的值。

我不明白你要做什么,但这应该有所帮助:

Sub Test()
    Dim ls As List
    Set ls = New List

     Set_val ls, 8
     Debug.Print (ls.next0 Is Nothing) 'True - why?
     Debug.Print ls.next0.val 'Error
End Sub

Sub Set_val(l As List, v As Integer)
     Set l.next0 = New List
     l.val = v
End Sub