English 中文(简体)
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

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.

问题回答

暂无回答




相关问题
Which name for a "smart" dictionary (hashtable)?

I m looking for a good name for a custom dictionary which automatically initializes the value for a requested key if it doesn t exist, using a delegate. The indexer implementation should help ...

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 = "...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Python dictionary simple way to add a new key value pair

Say you have, foo = bar d = { a-key : a-value } And you want d = { a-key : a-value , foo : bar } e = { foo :foo} I know you can do, d[ foo ] = foo #Either of the following for e e = { foo :foo}...

Storing ints in a Dictionary

As I understand, in Objective-C you can only put Objects into dictionaries. So if I was to create a dictionary, it would have to have all objects. This means I need to put my ints in as NSNumber, ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签