1
votes

Is it possible to set a dictionary object as a property of a class in classic ASP? I can't seem to get the syntax right. I have a function that I want to return a dictionary object, and assign the dictionary object that is returned by the function to the class property, but I keep getting a type mismatch error?

If I can't use the dictionary object, can I use an array?

I'm newish to classic ASP but I know C#/.NET pretty well.

2
"...assign it to the class property" - what is "it" in this case? The function, the dictionary object returned by the function, something else? - Martha
Make sure you are using the Set keyword properly. - Tester101
Thank you Tester101, that was my problem. I was doing this "m_objCategories = GetStaffCategories(m_intPhysicianID)" and I needed to do "Set m_objCategories = GetStaffCategories(m_intPhysicianID)" Sorry for the stupid question. - pinniger
There are no stupid questions, only stupid answers. - Tester101

2 Answers

7
votes

Here is a simple example

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

Just remember to use Set when working with objects.

0
votes

You should also use the Set keyword when assigning a property in the class.

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()