Re: fixed or dynamic array



Hi Peter,

"Peter T" <peter_t@discussions> wrote in message news:%23EwD3ZkkIHA.6092@xxxxxxxxxxxxxxxxxxxxxxx

The function Split was designed before VB had support for returning
arrays,
hence it returns a Variant. And because VB6 doesn't allow assigning of
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,
you'd
probably use the variant, and could point a strongly typed array at it
using
my 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 ;)





























.



Relevant Pages

  • Re: Split Function Creates Error 13 Type Mismatch
    ... A Variant can hold a string and an array of string. ... Dim vAs Variant ... As it is, was Variant accept an array of string returned by Array, but not by Split. ...
    (microsoft.public.access.modulesdaovba)
  • Re: Grouping shapes in Word
    ... The reason that it expects a Variant array as opposed to a String array is that ... a Variant can hold data of any type. ... Variant array of Shape names (string) or a Variant array of shape index numbers ... Dim avarShape() As Variant ...
    (microsoft.public.office.developer.vba)
  • Re: Sorting a variant array
    ... Dim tempList As Variant ... Dim testerList() As String ... Function sortTesters(rangeName As String) ... column range--but even that ends up as a x-rows by 1 column array. ...
    (microsoft.public.excel.programming)
  • Re: Populate listbox from array
    ... Static dbsAs String isn't required in your case: ... Static MyDeptArray As Variant ... > array. ... >> write a call-back function to populate your listbox. ...
    (microsoft.public.access.modulesdaovba)
  • Re: Paasing an array in OpenArgs
    ... Although the OpenArgs property is a variant, the Access 2003 help file indicates it must contain a string expression. ... I thought a good way to do this would be to create an array with the two date values and pass the array in the openargs argument. ...
    (microsoft.public.access.formscoding)

Quantcast