Subject: Re: TCOM with collection type objects - how to iterate throw all items? - DN [1]
cthuang@interlog.com (Chin Huang) - 16 Nov 1999 - comp.lang.tcl
In article <80res0$74t$1@news2.inter.net.il>,
SAP-OFEK <oleg.belilovsky@sap.com> wrote:
> I have a problem to iterate data collection object with a numeric index
> or with another way
Tcl Object to VARIANT Mapping
Values in Tcl are dual-ported objects. Tcl objects have a
string representation but also hold an internal representation
that can be manipulated more efficiently. For example, a Tcl
list is represented as an object that holds the list's string
representation as well as an array of pointers to the objects
for each list element. The two representations are a cache of
each other and are computed lazily. That is, each representation
is only computed when necessary, is computed from the other
representation, and, once computed, is saved. In addition, a
change in one representation invalidates the other one. As an
example, a Tcl program doing integer calculations can operate
directly on a variable's internal machine integer representation
without having to constantly convert between integers and
strings. Only when it needs a string representing the variable's
value, say to print it, will the program regenerate the string
representation from the integer. The internal representations
built into Tcl include boolean, integer and floating point
types.
When calling COM object methods, tcom tries to convert each
Tcl argument to the parameter type specified by the method
interface. For example, if a method accepts an int parameter,
tcom tries to convert the argument to that type. If the
parameter type is a VARIANT, the conversion has an extra
complication because a VARIANT is designed to hold many different
data types. One might simply copy the string representation
of the Tcl object to a string inside the VARIANT, and hope the
method implementation can interpret string correctly. This
approach doesn't work in general because there are implementations
that expect certain VARIANT types.
Tcom uses the Tcl object's internal representation type as a
hint for the result VARIANT type.
Tcl internal representation type VARIANT type
-------------------------------- ------------
boolean VT_BOOL
int VT_I4
double VT_R8
list one-dimensional array of VT_VARIANT
other VT_BSTR
Accessing Collection Elements
The standard interface for COM collections defines the Item
method for getting an element by specifying an index. Many
implementations of the method allow the index to be an integer
value (usually based from 1) or a string key. The index
parameter is a VARIANT, so you must account for the internal
representation type of the Tcl argument passed to that parameter.
# Assume $collection is a reference to a collection.
set element [$collection Item 1]
This command passes the string consisting of the single character
"1" to the Item method. The method may return an error because
it can't find an element with that string key.
set numElements [$collection Count]
for {set i 1} {$i <= $numElements} {incr i} { ;# 1
set element [$collection Item $i] ;# 2
}
In line 1, the for command sets the internal representation of
$i to an int type as a side effect of evaluating the condition
expression {$i <= $numElements}. The command in line 2 passes
the integer value in $i to the Item method, which should succeed
if the method can process integer index values.
Last modified
1999-11-25
1999-11-25
(195.108.246.50)
Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated
