是否可以在classic ASP中将字典对象设置为类的属性?我似乎无法正确使用语法。我有一个函数,我想返回一个字典对象,并将函数返回的字典对象分配给类属性,但我不断收到类型不匹配错误?
如果我不能使用字典对象,我可以使用数组吗?
我对经典ASP很新,但我非常了解C#/ .NET。
答案 0 :(得分:7)
这是一个简单的例子
Class TestClass
private testDictionary
public property get TestDict
Set TestDict = testDictionary
end property
public property let TestDict(byval value)
Set testDictionary = value
end property
public property set TestDict(byval value)
Set testDictionary = value
end property
public function getValue(index)
getValue = testDictionary.Item(index)
end function
end class
'Create a Dictionary and add an entry
Set newDict = CreateObject("Scripting.Dictionary")
newDict.Add 1, "This is a test"
'Assign the dictionary to the custom object
Set mynewClass = new TestClass
mynewClass.TestDict = newDict
Set newDict = Nothing
wscript.echo mynewClass.getValue(1)
Set mynewClass = Nothing
请记住在处理对象时使用设置。
答案 1 :(得分:0)
在课堂上分配属性时,您还应该使用Set关键字。
Class DictionaryClass
Private m_Dictionary
Public Sub Class_Initialize()
Set m_Dictionary = Server.CreateObject("Scripting.Dictionary")
End Sub
Public Property Get Dictionary()
Set Dictionary = m_Dictionary
End Property
Public Property Set Dictionary(value)
Set m_Dictionary = value
End Property
End Class
Function GetDictionary()
Dim dictionary : Set dictionary = Server.CreateObject("Scripting.Dictionary")
'some magic'
Set GetDictionary = dictionary
End Function
Dim oDictionaryClass : Set oDictionaryClass = New DictionaryClass
Set oDictionaryClass.Dictionary = GetDictionary()