Mapping the Parameter Classes
To get an overall understanding of how the parameter classes are organized, mapping out the parameters is a good way to start. This section shows you the code to print out all the report parameters and their properties.
The code to print out the parameters is very simple. The classes are composed of two collections. There is a collection for storing the parameter fields, and there is a collection for storing the values that each parameter has. The main logic consists of For Each loops to process each element in the collection.
The ReportDocument class is the only class that is mapped in this chapter because it is the most frequently used class for modifying reports. Mapping out the CrystalReportViewer class would prove to be mostly redundant.
Parameters in the ReportDocument class are managed with two collection classes. The ParameterFieldDefinitions collection stores all the parameters as ParameterFieldDefinition objects. The ParameterValues collection manages the value objects within each parameter of type ParameterValue. Listing 16-1 shows the code for mapping out the parameters.
Dim MyReport As New CrystalReport1() MapParameterFieldDefinitions(MyReport.DataDefinition.ParameterFields, 0)
...
Sub MapParameterFieldDefinitions(ByVal ParameterFieldDefintions As _
CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinitions, _
ByVal Indent As Integer)
Dim ParameterFieldDefinition As _
CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition
For Each ParameterFieldDefinition In ParameterFieldDefintions
MapParameterFieldDefinition(ParameterFieldDefinition, Indent)
Next
End Sub
Sub MapParameterFieldDefinition(ByVal ParameterFieldDefinition As _
CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition, _
ByVal Indent As Integer)
Output("Name: " & ParameterFieldDefinition.ParameterFieldName, Indent)
Output("PromptText: " & ParameterFieldDefinition.PromptText, Indent + 2)
Output("ValueType: " & _
ParameterFieldDefinition.ParameterValueKind.ToString(), Indent + 2)
Output("Kind : " & _
ParameterFieldDefinition.DiscreteOrRangeKind.ToString(), Indent + 2)
If ParameterFieldDefinition.CurrentValues.Count > 0 Then
Output("Current Values", Indent + 2)
MapParameterValues(ParameterFieldDefinition.CurrentValues, Indent + 4)
End If
If ParameterFieldDefinition.DefaultValues.Count > 0 Then
Output("Default Values", Indent + 2)
MapParameterValues(ParameterFieldDefinition.DefaultValues, Indent + 4)
End If
End Sub
Sub MapParameterValues(ByVal ParameterValues As _
CrystalDecisions.Shared.ParameterValues, ByVal Indent As Integer)
Dim ParameterValue As Object
For Each ParameterValue In ParameterValues
If TypeOf ParameterValue Is _
CrystalDecisions.Shared.ParameterRangeValue Then
MapParameterRangeValue(CType(ParameterValue, _
CrystalDecisions.Shared.ParameterRangeValue), Indent)
Else
MapParameterDiscreteValue(CType(ParameterValue, _
CrystalDecisions.Shared.ParameterDiscreteValue), Indent)
End If
Next
End Sub
Sub MapParameterDiscreteValue(ByVal ParameterValue As _
CrystalDecisions.Shared.ParameterDiscreteValue, ByVal Indent As Integer)
Output("Value : " & ParameterValue.Value.ToString(), Indent)
End Sub
Sub MapParameterRangeValue(ByVal ParameterValue As _
CrystalDecisions.Shared.ParameterRangeValue, ByVal Indent As Integer)
Output("Value : " & ParameterValue.StartValue.ToString() & _
" to " & ParameterValue.EndValue.ToString(), Indent)
End Sub
The first procedure, MapParameterFieldDefinitions(), loops through every parameter object in the ParameterFieldDefinitions collection and calls MapParameterFieldDefinition(). This procedure prints out a few properties (you can add more if you wish) and then calls MapParateterValues() for both the CurrentValues and DefaultValues collections. Each of these collections are of the data type ParameterValues.
The MapParameterValues() procedure loops through every parameter value. Notice that the variable it uses for the loop is of type Object. As you will see in the section on programming the classes, parameter values are added using either the ParameterDiscreteValue class or the ParameterRangeValue class. However, at this point we don’t know which class was used. So we declare a generic Object variable and then use the TypeOf keyword to see which interface it supports. This lets us call the proper procedure to map it out.
To see the output, I ran this code against a report that has two parameters. The first parameter is a string that can accept a single value and has no default values. The second parameter is a date range that has multiple default values. Notice that even though the date range has default values that are all discrete. The output is shown in Figure 16-3.
Figure 16-3. The output from mapping the parameters.