Collection of objects in classic ASP using VBScript


I have a class called 'Company' that has properties like 'CompanyName', 'CompanyCode' and 'IsActive'. This class is in VBScript. I want to store a collection of Company objects using VBScript in classic ASP. Is this possible, and if yes, then how would I do it?

Best Solution

You can use an array or a dictionary object:


' create an array with a fixed size
dim companies(2) 

' fill the array with the companies
set companies(0) = Company1 
set companies(1) = Company2
set companies(2) = Company3

' iteration example 1
dim company
for each company in companies
    response.write company.CompanyName

' iteration example 2
dim i
for i = 0 to ubound(companies)
    response.write companies(i).CompanyName


' create a dictionary object
dim companies
set companies = server.createObject("Scripting.Dictionary")

' add the companies
companies.add "Key1", Company1
companies.add "Key2", Company2
companies.add "Key3", Company3

' iteration example
dim key
for each key in companies.keys
    response.write key & " = " & companies.item(key).CompanyName
Related Question