views:

141

answers:

1

Hi

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?

+3  A: 

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.

AnthonyWJones
Or you can have a nice pass by value side effect, when calling a procedure with callCall MySyb(p1,p2)Oh, how I miss the joy of VBScript...
Yann Schwartz
ASP with JScript makes this much easier and if your from C# it should be a nicer swap over :)
Pete Duncanson
Thanks. I assume the first line must read 'For Each key in colPayments.Keys' ? And I'm not using the ordinal, it's just that the Dictionary requires a key.
edosoft
Also good call on 'Avoid parentheses when using a procedure statement'
edosoft
You can use the "system.collection.arraylist" object from the .net framework in classic ASP if you need a List implementation.
Joost Moesker
@edo.dosoft.nl: Isn't a need to put .Keys in the For Each. As stated enumerating a Dictionary will enumerate the Keys.
AnthonyWJones
@Pete: Yes in many ways JScript is a better choice. However purely on the basis that the vast majority of example ASP code out there is in VBScript using JScript just seems to be harder work than using VBScript. Not quite sure why exactly its like that perhaps its because VBScript, The Variant type and COM components have grown up together and seem to look right together. COM coding (such as ADODB) just never seems to look right in JScript.
AnthonyWJones
@Anthony: There are a few bits where it struggles a bit Enumerators on some of the build in objects are a good point. That said I think JS makes ASP much more usable and I just like pushing ASP with Jscript, as you've probably seem on a few other posts ;) Might be time for me to start up some blog posts on how to do it with some better examples...
Pete Duncanson
@Pete: I'd rather be pushing "Stop using ASP classic". Adding new JScript solutions to an existing VBScript one doesn't seem very sensible. If I were starting a new project from scratch I wouldn't use ASP classic at all. I've always liked the idea of ASP/JScript but it just never got to critical mass and that boat has well an truely sailed now.
AnthonyWJones
There needs to be a public service announcement informing people to use Set when assigning objects to variables. It has to be the most common problem in VBScript code posted on StackOverflow.
Tester101