Coding tips (VBScript)

Using default properties

The most common properties in the Crystal Enterprise object model have been designated as default properties. Default properties can help you drastically reduce the amount of typing you need to do when writing code using VBScript.

Tip: These default properties are marked with a blue bullet in the VB Object Browser.

Example 1: InfoObjects collection

Dim InfoObjects

Set InfoObjects = InfoStore.Query("SELECT SI_NAME, SI_ID, SI_PARENT_FOLDER FROM CI_INFOOBJECTS WHERE SI_PROGID = 'CrystalEnterprise.Report'")


Dim Report

These two lines are equivalent

Set Report = InfoObjects.Item(1)

Set Report = InfoObjects(1)

If you want to get a property from the first object in the collection, you can use any of the following:

ParentFolder = InfoObjects.Item(1).Properties("SI_PARENT_FOLDER").Value

ParentFolder = InfoObjects.Item(1).Properties("SI_PARENT_FOLDER")

ParentFolder = InfoObjects.Item(1)("SI_PARENT_FOLDER")

ParentFolder = InfoObjects(1)("SI_PARENT_FOLDER")

ParentFolder = InfoObjects()("SI_PARENT_FOLDER")

This kind of code reduction is possible because:

The last line of code demonstrates how you can even omit the 'Index' parameter (1). If you look at the definition of the Index parameter of all Item properties, you will see that the parameter is optional. If you omit the Index, you will get the first object in the collection.

Example 2: Property bags

The following expressions—which set a property contained inside another property, which is contained inside yet another property—are also equivalent:


Dim InfoObj as InfoObject

InfoObj.Properties.Item("SI_PATH").Properties.Item("SI_FOLDER_NAME1").Value = "my name"

InfoObj.Properties("SI_PATH").Properties("SI_FOLDER_NAME1").Value = "my name"

InfoObj("SI_PATH").Properties("SI_FOLDER_NAME1") = "my name"


Note:    Property bags consist of an array of related properties, where there is a property that contains the number of items (servers, logon info, prompts), and the items themselves, in this form:

Using For vs. For...Each

When iterating in VBScript, it is more efficient to use For...Each than a regular For loop. This holds true for properties such as Count and Item.

Method 1: For loop



Dim infoOb as InfoObject

Dim prop

for i = 1 to infoOb.Properties.Count

    set prop = infoOb.Properties(i)

    Response.Write "Property Name: " & prop.Title

next



Method 2: For...Each



Dim infoOb as InfoObject

Dim prop

for each prop in infoOb.Properties

    Response.Write "Property Name: " & prop.Title

next



In Method 2, the For...Each construct eliminates an entire line, which means that for each iteration, less work needs to be done.



Crystal Decisions, Inc.
http://www.crystaldecisions.com
Support services:
http://support.crystaldecisions.com