Re: fixed or dynamic array
- From: "Mike Williams" <mikea@xxxxxxxxxxxxxxxxx>
- Date: Sun, 30 Mar 2008 11:02:52 +0100
"Peter T" <peter_t@discussions> wrote in message news:%23EwD3ZkkIHA.6092@xxxxxxxxxxxxxxxxxxxxxxx
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
arrStr = Split(s, deLim) ' works
v = Split(s, deLim) ' type mismatch
The first one works [arrStr = Split etc] only because arrStr is a dynamic array (an array for which it is possible to change the number of elements at runtime). It is a dynamic array because you use Redim (as opposed to Dim) to create it. The fact that you gave it elements (0 to e) is irrelevant. For example, if you used Redim arrStr (0 to 99) and the Split function found 6 substrings then arrStr would have only 6 elements after the Split, arrStr(0) to arrStr(5). The fact that you initially gave it 100 elements (0 to 99) is totally irrelevant. If you had instead used Dim arrStr (0 to e) then the Split function would have failed, because arrStr would be a static array and the Split function needs a dynamic array.
The second one fails [v = Split(s, deLim)] because v is not simply a Variant. It is an *array of Variants*. The Split function needs either a dynamic array (as in the first case) into which it will place its output or a normal Variant into which it will place a string array containing its output. For that code to work you would need to have declared v as a normal Variant, rather than an array of them, as in Dim v as Variant [rather than Dim v (0 to e) as Variant]. In simple terms, you can get the output of Split to go into a dynamic String array or into a standard Variant or into one element of array of Variants. Have a look at this:
Private Sub Command1_Click()
ReDim arrstr(0 To 12) As String ' the 0 to 12
Dim j As Variant ' left in here just to show
Dim v(0 To 12) As Variant ' what happens
Dim s As String, deLim As String
deLim = ":"
s = "12 : 34 : 56 : 78"
'
arrstr = Split(s, deLim)
Print LBound(arrstr), UBound(arrstr)
'
j = Split(s, deLim)
Print LBound(j), UBound(j)
'
v(12) = Split(s, deLim)
Print LBound(v(12)), UBound(v(12))
End Sub
Mike
.
- Follow-Ups:
- Re: fixed or dynamic array
- From: Mike Williams
- 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
|