ComponentOne True DataControl 8.0
Working with Objects and Collections

This section describes how to work with objects and collections in Visual Basic code, with an emphasis on efficiency.  Although the concepts are illustrated with True DataControl objects and collections, you can apply the same fundamentals to all Visual Basic objects and collections.

You can reference an object in a collection using its zero-based index.  For example, the first field in a TData control has an index value of 0.  You can read or set the corresponding Field object's properties as follows:

Example Title
Copy Code
' Read a property of Field

variable = TData1.Fields(0).Property

' Set a property of Field

TData1.Fields(0).Property = variable

The Fields property returns an object of type Fields, which is a collection of Field objects. The Fields property is the default (implicit) property of the TData control. This means that the name of the collection, Fields can be omitted, so the previous example (with Name as the field’s property) can be abbreviated as follows:

Example Title
Copy Code
' Read the field’s Name

variable = TData1(0).Name

' Set the field’s Name

TData1(0).Name = variable

This does not apply to other properties, only the Fields property name can be omitted this way.

Or, if we take another property of the Field object, the Value property, it will become the following:

Example Title
Copy Code
' Read the field’s value

variable = TData1(0).Value

' Set the field’s value

TData1(0).Value = variable

The Value property is the default (implicit) property of the Field object. That’s why the Value property name can be omitted and the preceding example re-written as

Example Title
Copy Code
' Read the field’s value

variable = TData1(0)

' Set the field’s value

TData1(0) = variable

Another implicit property in True DataControl is the Value property of the Parameter object. It also can be omitted in code. All other properties and collections must be referenced in full.

You can create a reference to an object in a collection using the collection's Item property.  The following code creates a reference to the first field:

Example Title
Copy Code
' Declare Field0 as a Field object

Dim Field0 As TrueData60Ctl.Field

' Set Field0 to reference the first Field in the collection

Set Field0 = TData1.Fields.Item(0)

Note the use of the type library qualifier TrueData60Ctl in the preceding example.  Using the type library qualifier is recommended in order to resolve potential naming conflicts with other controls.  For example, if you use another control in the same project that also defines an object named Field, the TrueData60Ctl type library qualifier is required, as is the type library qualifier for the other control.

Since the Item property is implicit for collections, you can omit it:

Example Title
Copy Code
' Declare Field0 as a Field object

Dim Field0 As TrueData60Ctl.Field

' Set Field0 to reference the first Field in the collection

Set Field0 = TData1.Fields(0)

You can now use Field0 to read or set the Field object's properties:

Example Title
Copy Code
variable = Field0.Property    ' Read a property

Field0.Property = variable    ' Set a property

Very often, you need to read and set more than one of an object's properties.  For example:

Example Title
Copy Code
' Read a Field object's properties

variable1 = TData1.Fields(0).Property1

variable2 = TData1.Fields(0).Property2

' Set a Field object's properties

TData1.Fields(0).Property1 = variable1

TData1.Fields(0).Property2 = variable2

This code is very inefficient because each time the object TData1.Fields(0) is accessed, Visual Basic creates a reference to the object and then discards it after the statement is completed.  It is more efficient to create a single reference to the object up front and use it repeatedly:

Example Title
Copy Code
' Declare Field0 as a Field object

Dim Field0 As TrueData60Ctl.Field

' Set Field0 to reference the first Field in the collection

Set Field0 = TData1.Fields(0)

' Read a Field object's properties

variable1 = Field0.Property1

variable2 = Field0.Property2

' Set a Field object's properties

Field0.Property1 = variable1

Field0.Property2 = variable2

This code is much more efficient and also easier to read. If your Visual Basic application accesses collection objects frequently, you can improve the performance of your code significantly by adhering to these guidelines.

Similarly, you can apply this technique to other objects and collections of True DataControl, and of Visual Basic in general.

The most important thing is to use this technique with the Recordset property, since it is often used in performance-critical loops. It’s a very poor programming practice to write code like

Example Title
Copy Code
variable = TData1.Recordset.Fields(0)

If you need to make repetitive calls to the Recordset property, set up a variable for it, as in the following example

Example Title
Copy Code
Dim RS As ADODB.Recordset

Set RS = TData1.Recordset

variable = RS(0)

Visual Basic also provides an efficient With...End With statement for setting multiple properties of an object without explicitly assigning it to a variable.  For example, the following code sets multiple properties of the first field (recall that collections are zero-based):

Example Title
Copy Code
With TData1.Fields(0)

    .Property1 = variable1

    .Property2 = variable2

End With

Two of the True Data collections, Fields and Parameters (returned by the Fields and Parameters properties, respectively) allow you to reference their members by name.  So you can reference a Field or a Parameter object using either its index or its name.  Thus, the following statements are equivalent:

Example Title
Copy Code
' Declare Field0 as a Field object

Dim Field0 As TrueData60Ctl.Field

' Reference by numeric index

Set Field0 = TData1.Fields(0)

' Reference by field name

Set Field0 = TData1.Fields("LAST")

To create and add an object to a collection, use the collection's Add method.  For example, you can create more fields by adding new Field objects to the Fields collection:

Example Title
Copy Code
' Create a field object with index 0

Dim F As TrueData60Ctl.Field

Set F = TData1.Fields.Add(0)

This code adds a Field object with index 0 to the Fields collection of TData1.  The original TData1.Fields(0) object now has an index of 1.  Alternatively, you can create a Field object with index 1:

Example Title
Copy Code
' Create a field object with index 1

Dim F As TrueData60Ctl.Field

Set F = TData1.Fields.Add(1)

Note that the Add method of the Fields collection is used like a function, with its arguments (here, the field index) enclosed in parentheses.  Also, since the Add method always returns a reference to the Field object that was just created, you must precede the assignment statement with the Visual Basic Set keyword.

To remove an existing item from a collection, use the Remove method:

Example Title
Copy Code
' Remove the field object with index 1

TData1.Fields.Remove 1

After this statement is executed, all fields with collection indexes greater than 1 will shift down by 1 to fill the place of the removed field.  Note that the Remove method is called like a subroutine---its argument is not enclosed in parentheses.

You can determine the number of objects in a collection using the collection's Count property:

Example Title
Copy Code
' Set a variable to the number of fields in TData1

variable = TData1.Fields.Count

You can also iterate through all objects in a collection using the Count property as in the following example, which prints the name of each field in a True DataControl:

Example Title
Copy Code
For n = 0 To TData1.Fields.Count - 1

    Debug.Print TData1.Fields(n).Name

Next n

The Count property is also useful for appending and removing objects:

Example Title
Copy Code
' Determine how many fields there are

Dim NumFields As Integer

NumFields = TData1.Fields.Count

' Append a field to the end of the Fields collection

Dim F As TrueData60Ctl.Field

Set F = TData1.Fields.Add(NumFields)

' The following loop removes all fields

While TData1.Fields.Count <> 0

TData1.Fields.Remove 0

Wend

Visual Basic also provides an efficient For Each...Next statement that you can use to iterate through the objects in a collection without using the Count property:

Example Title
Copy Code
Dim F As TrueData60Ctl.Field

For Each F In TData1.Fields

    Debug.Print F.Name

Next S

In fact, using the For Each...Next statement is the preferred way to iterate through the objects in a collection.

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback