我有一個自定義類List:
Private me_col As New Collection
Public Function GetAt(position As Integer)
GetAt = me_col(position 1)
End Function
Public Sub Add(value)
me_col.Add value
End Sub
如果我直接在集合中添加一個串列,我可以通過 index.js 獲取它。訊息框將顯示“串列”:
Sub Run()
Dim col As New Collection
Dim child As New List
col.Add child
MsgBox TypeName(col(1))
End Sub
但是,如果我嘗試通過添加到串列List.Add和GetAt之后,應用程式將引發一個運行時錯誤“438”:物件不支持此屬性或方法:
Sub Run()
Dim col As New List
Dim child As New List
col.Add child
MsgBox TypeName(col.GetAt(0))
End Sub
這適用于原始值,僅在自定義類等物件上失敗。
col.Add "a primitive value"
MsgBox TypeName(col.GetAt(0)) 'String
uj5u.com熱心網友回復:
請嘗試下一個方法:
- 以這種方式調整“List”類代碼:
Option Explicit
Private me_col As New Collection
Public Function GetAt(position As Integer) As Variant
If IsObject(me_col(position 1)) Then
Set GetAt = me_col(position 1) 'for the case of an object member (class, collection, sheet etc.)...
Else
GetAt = me_col(position 1)
End If
End Function
Public Sub Add(value)
me_col.Add value
End Sub
- 用下一個方法測驗它:
Sub RunList()
Dim col As New List, child As New List
child.Add "first item" 'place an item in the first class collection
col.Add child 'place the class inside the class
col.Add "second item" 'place a string like the second element
col.Add Array("third item", "fourth item") 'add an array like the third element
MsgBox col.GetAt(0).GetAt(0) 'being a class, you should return the string in the appropriate way...
MsgBox col.GetAt(1) 'returning the string entry/element
MsgBox col.GetAt(2)(0) 'returning the first element of the array element...
End Sub
請測驗它,嘗試理解它的含義并發送一些反饋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/336787.html
下一篇:使用Webpack時未定義d3
