The VBScript dictionary is a great tool to work with in an object-oriented software
development project; especially when the object you are creating will have some sort of
collection of records.
If you are like me and dislike extraneous lines of code, then when dealing with VBScript
dictionaries you have certainly discovered that trying to iterate through the collection like
an enumerated list requires extra lines of code like:
dim aDict, ibound
aDict = myDictionary.items
ibound = ubound(aDict) '// query the ubound once for our loop
for x = 0 to ibound
'... do something ...
next
Wouldn't it be nice if there was a way to simply dive right into the loop? What about a
great way to page the data into manageable chunks? Typically, this is done directly from
a Record Set - wasting valuable server resources and time.
There are many pitfalls that the typical methods of iteration and paging fail to address, or
fail to make simple for a programmer, causing more programming that should be
necessary. Not too mention, object-oriented programming is weakly implemented in
VBScript, lacking powerful features like prototyping and inheritance.
With the new cCollection class, iterating through key/value pairs, and paging key/value
pairs in VBScript Dictionary objects is very simple.
I have created a class that acts very much like a VBScript Dictionary with most of the
same methods, but with the added ability to access key/value pairs using a number
instead of a key when accessing an item. Normal VBScript dictionaries do not allow this:
<%
set dict = server.createobject("scripting.dictionary")
That code will not write "zombie" to the browser because there is no key 0 in dict.
Instead, ASP will just return an Empty value.
In a typical environment, dictionaries cannot be directly enumerated. However, by
instantiating a copy of my cCollection class, and adding items, or using the .Fill()
method to populate the collection with a preexisting Dictionary, developers can get this
functionality cleanly.
My new class extends (if you true OOP'ers will forgive my use of the word) the old
Dictionary object. Now response.write dict.item(0) will print "zombie" to the
browser!
To use the class, download the code, and include it in your script:
<!-- #include file="class_collection.asp" -->
This great tool comes with some caveats:
The shorthand form dict("key") is lost. You must always access the item by using the .item property.
For example: collection.item(X)
Paging
I have even taken this project a step further, making it better than your average dictionary
by creating new properties and methods that making paging your data easy, without
clutter (hurray encapsulation!).
The newest version of this class includes some new properties:
pageSize
pagingStart
pagingEnd
pagesTotal
And two new methods:
PagingString_Theme1
PagingString_Theme2(arg1)
When you page data, there are usually two sections that are present on the page: a set
of hyperlinks that serves as page navigation, and a list of records that starts on the first
record of the current page and ends at the last record of the current page.
Here is an example of how to begin using the paging features of cCollection.
<!-- #include file="class_collection.asp" -->
<%
'//--------------------------------------------
'// create a new instance of cCollection
'//--------------------------------------------
set myCollection = new cCollection
'//--------------------------------------------
'// add records to the collection
'//--------------------------------------------
We begin paging our records by setting the pageSize property.
myCollection.pageSize = 4 '// or any integer
If you do not set the pageSize, all records will be within the page, effectively giving you
one page of records. In this example, the pageSize would effectively become 26 had we
not set it explicitly to four (4).
The navigation needed to work your way through the pages of records can be easily
written to the browser by using the PagingString_ThemeX methods of class cCollection.
Method .PagingString_Theme1 returns an HTML string of <li>'s. The
pageSize determines how many pages of data there will be. The output
includes hyperlinked page numbers, with the active page highlighted by braces "[]". Each
hyperlink uses "pg=" as a querystring parameter. Employed by the example, it would
return:
Method .PagingString_Theme2(arg1) returns an HTML string of <li>'s. Based on the
pageSize, produces a linear count of how many pages of data there will be. The output
includes hyperlinked page numbers, with the active page highlighted by braces "[]". Each
hyperlink uses "pg=" as a querystring parameter. A "Go to first page", "Go to previous
page", "Go to next page", and "Go to last page" link is added to the output to make
scrolling through the pages easier. The argument, arg1, is supposed to be an integer. It
determines how many pages will precede and proceed the active page in the list.
Employed by the example, and given an argument of 2, it would return:
While on page one (1), the double-arrow link will not be active, nor will the single-arrow
link because page one is as far back as the paging goes. Two pages appear to the right of
page one, because of the parameter we supplied to the method. The first single arrow
link after page three will take the user to page two (2), and the double arrow link after
page three will take the user to the last page of records, page seven (7).
If the user clicked on page four(4) the output of the method would be:
To iterate through the collection, we make use of the pagingStart and pagingEnd
properties.
for x = myCollection.pagingStart to myCollection.pagingEnd - 1
response.write myCollection.item(x) & "<br />"
next
All records for the page of data will be response.write'en to the browser. But what if
you wanted to show 13 records per page? It's simple! Just set the pageSize property
before you begin your iteration:
YourCollectionObject.pageSize = 13
No matter when you add values to the collection, the object will always remain accurate.
This script is very useful, and will take a lot of the work out of creating robust, objectoriented
scripts that need enumerable collections.
See the accompanying zip file for a sample of the script that can be copy/pasted to
your ASP web server.
About the Author
You can learn more about the author and provide feedback via his web site located at http://www.nicodemas.com.