views:

356

answers:

2

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.

+3  A: 

Here is a simple example

Class TestClass
    private testDictionary

    public property get TestDict
     Set TestDict = testDictionary
    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.

Tester101
A: 

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