开发者

Using Classes in a Dictionary in Classic ASP

I usually do C# but have inherited a classic ASP project.

I have defined a class:

    Class clsPayment    
        Public Name
    End Class

Set objPayment = New clsPayment
objPayment.Name = "StackOverflow payment"    

And a dictionary:

Set colPayments = CreateObject("Scripting.Dictionary")
colPayments.Add 1, objPayment 

When reading the objects later on I cannot call the public field:

For i = 0 to colPayments.Count - 1
    objPayment = colPayments.Item(i)
    Response.Write(objPayment.Name)
Next    

This throws an error on the Response.Write line:

Microsoft VBScript runtime error '800a01a8'

开发者_C百科

Object required: ''

What did I do wrong?


Change your for loop to this:-

For Each key in colPayments
    Set objPayment = colPayments(key)
    Response.Write objPayment.Name
Next

There are several things that need point out.

Use Set to assign an object. In VBScript it is necessary to use the Set keyword as above when assign an object to a variable.

Dictionary isn't really a collection You seemed to be attempting to use the Dictionary as an ordinal collection, it isn't. The Scripting dictionary is strictly an associative array and there is no dependable ordering. Unlike the .NET Dictionary For Each on the object returns an enumeration of just the keys used not a KeyValue pair..

Avoid parentheses when using a procedure statement Note that the call to Response.Write has the parantheses missing. Whilst you will often see these used successfully when there is only one parameter (its interpreted as an expression) it will be a syntax error when 2 or more parameters are needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜