我正在尝试使用一个我在网上找到的函数按照对象属性(即Id)对字典进行排序,但是在这个
VBScript类:
For Each i In dict
行中,我收到了这个错误信息:Microsoft VBScript运行时错误:对象不支持此属性或方法。我尝试了For Each i In dict.Items
,但是我得到了相同的“dict.Items”错误消息。我使用的是较旧版本的VBScript,因此没有像dict.Count
这样的功能。VBScript类:
Class TestClass
Public ID
Public TestText
Private Sub Class_Initialize
TestText = ""
End Sub
End Class
Set gDic = CreateObject("Scripting.Dictionary")
For i = 1 to 5
Set temp = new TestClass
temp.ID = i
temp.TestText = "Test" & i
gDic.Add i,temp
Next
Set NewDic = SortDict(gDic)
msgbox NewDic.Items()(1).TestText
排序函数:
Function SortDict(ByVal dict)
Dim i, j, temp
For Each i In dict
For Each j In dict
If(dict.Item(i) <= dict.Item(j)) Then
temp = dict.Item(i)
dict.Item(i) = dict.Item(j)
dict.Item(j) = temp
End If
Next
Next
Set SortDict = dict
End Function
For Each i In dict.keys
。如果这不起作用,请尝试使用字符串键 (gDic.Add CStr(i), temp
)。 - Salman A