我正在测试VBA中面向对象的功能,我遇到了如何使用参数创建新对象的问题。由于VBA构造函数不允许参数,我正在调整从Java到VBA的静态工厂方法。代码如下:
CNode类:
Option Explicit
Private pValue As Variant
Private pNode As CNode
' "Constructor" for factory method
Public Sub newNode(value As Variant, node As CNode)
pValue = value
Set pNode = node
End Sub
' "Print entire LinkedList"
Public Sub printList()
Dim aux As CNode
Set aux = pNode
While (pNode Is Not Nothing)
Debug.Print aux.pValue
Set aux = aux.pNode
Wend
End Sub
和Factory模块(我相当于“static”关键字):
Option Explicit
Public Function createNode(value As Variant)
Dim n As CNode
Set n = New CNode
n.newNode value, Nothing
Set createNode = n
End Function
例如,当我设置root = Factory.createNode(10)时,我得到的错误是“对象变量或未设置块变量”。
我做错了什么?