Re: fixed or dynamic array
- From: "Bill McCarthy" <Bill@xxxxxxxxxx>
- Date: Sun, 30 Mar 2008 20:41:23 +1100
Hi Peter,
"Peter T" <peter_t@discussions> wrote in message news:%23EwD3ZkkIHA.6092@xxxxxxxxxxxxxxxxxxxxxxx
arrays,
The function Split was designed before VB had support for returninghence it returns a Variant. And because VB6 doesn't allow assigning ofyou'd
references to an array, the assignment to an array causes a copy of the
data. So for large result sets, it's probably best not to assign it to a
strongly typed string array. For max performance in large data cases,
probably use the variant, and could point a strongly typed array at itusingmy SafeArray class (search Karl's site for a copy)
Illuminating, thanks.
Only curiosty but are you able to explain why assigning Split to a pre
dimensioned variant array fails, in contrast to a similarly dimensioned
string array.
Redim arrStr(0 to e) As String
ReDim v(0 to e) as Variant
' where e is the know no. of elements in advance
assStr = Split(s, deLim) ' works
v = Split(s, deLim) ' type mismatch
I think you've misunderstood, Split returns a Variant, not an array of Variants. The Variant is a 12 or 16? byte structure that contains information about what it contains (VARTYPE) as well as either the data or a pointer to the data. In this case that's a pointer to the array which itself is a strongly typed OLE SafeArray that contains the strings.
So when you assign that variant to a strongly typed array of strings, eg:
Dim s() As String
s = Split(s, deLim)
VB calls on SafeArrayCopy, and a complete copy of the array the variant points to is made.
The reason you would want to undertake this copying is only when copying is less expensive than subsequent iteration. But really both have overheads. However, pointing a strongly typed array to the data, avoids copying the data and still provides fast access. A variant containing an array is typically slow access because Vb at runtime has to check the VARTYPE and deiced where the (i) is an array index call or a default item call etc. So on each iteration there can be some overhead. Possibly, a For Each loop might avoid that overhead (I haven't checked), but a For i = x To y style of loop has overhead with a variant.
Now onto your question, VB doesn't support conversion of arrays, you have to convert each item. So an array of variants is not assignable to or from an array of strings. (NOT to be confused with a variant containing an array of strings)
Honestly, as I look at the mess above I just wrote, I see why at times I prefer .NET for this kind of thing where arrays are reference types, and there's none of this variant nonsense ;)
.
- Follow-Ups:
- Re: fixed or dynamic array
- From: Peter T
- Re: fixed or dynamic array
- References:
- fixed or dynamic array
- From: George Hester
- Re: fixed or dynamic array
- From: Mike Williams
- Re: fixed or dynamic array
- From: Peter T
- Re: fixed or dynamic array
- From: Bill McCarthy
- Re: fixed or dynamic array
- From: Peter T
- fixed or dynamic array
- Prev by Date: Re: fixed or dynamic array
- Next by Date: Re: fixed or dynamic array
- Previous by thread: Re: fixed or dynamic array
- Next by thread: Re: fixed or dynamic array
- Index(es):
Relevant Pages
|