ASP 101 - Active Server Pages 101 - Web03
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash


 Top ASP 101 Stories Top ASP 101 Stories
What is Adovbs.inc and Why Do I Need It?
An Overview of ASP.NET
Connections, Commands, And Procedures

QUICK TIP:
Using Cookies in your ASP page
Show All Tips >>


Extending VBScript's Dictionary Object

by Robert Simpson

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")

dict.add "z", "zombie"
dict.add "g", "ghost"
dict.add "v", "vampire"

response.write dict.item(0)
%>

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
'//--------------------------------------------

myCollection.add "a", "Alpha"
myCollection.add "b", "Bravo"
myCollection.add "c", "Charlie"
myCollection.add "d", "Delta"
myCollection.add "e", "Echo"
myCollection.add "f", "Foxtrot"
myCollection.add "g", "Golf"
myCollection.add "h", "Hotel"
myCollection.add "i", "India"
myCollection.add "j", "Juliet"
myCollection.add "k", "Kilo"
myCollection.add "l", "Lima"
myCollection.add "m", "Mike"
myCollection.add "n", "Nicodemas"
myCollection.add "o", "Oscar"
myCollection.add "p", "Papa"
myCollection.add "q", "Quebec"
myCollection.add "r", "Romeo"
myCollection.add "s", "Sierra"
myCollection.add "t", "Tango"
myCollection.add "u", "Uniform"
myCollection.add "v", "Victor"
myCollection.add "w", "Whiskey"
myCollection.add "x", "X-ray"
myCollection.add "y", "Yankee"
myCollection.add "z", "Zulu"
%>

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:

<li>[1]</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>

As the user clicked through the pages, the braces would move around the current page:

<li>1</li>
<li>2</li>
<li>[3]</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>

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:

<li>&lt;&lt;</li>
<li>&lt;</li>
<li>[1]</li>
<li>2</li>
<li>3</li>
<li>&gt;</li>
<li>&gt;&gt;</li>

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:

<li>&lt;&lt;</li>
<li>&lt;</li>
<li>2</li>
<li>3</li>
<li>[4]</li>
<li>5</li>
<li>6</li>
<li>&gt;</li>
<li>&gt;&gt;</li>

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.


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES