ps. I think it's the split function and the "dim asSplit() as String" that's
doing the damage.
I could step through this code with watches on aaa and see that it keeps its
values.
Option Explicit
Sub Test()
Dim a As Variant
'The first call works OK
a = fnFruitArray
'MsgBox a(0)
'But the second call doesn't return an initialised array
a = fnFruitArray
'MsgBox a(0)
End Sub
Function fnFruitArray() As String()
Static bBeenHere As Boolean
Static aaa() As String
Static asSplit() As String
If bBeenHere = False Then
ReDim aaa(1 To 5)
aaa(1) = "asdf"
asSplit = Split("apple,orange,banana", ",")
bBeenHere = True
End If
fnFruitArray = asSplit
End Function
Dave Peterson wrote:
>
> If I change it to:
> Static asSplit As Variant
>
> It worked fine.
>
> (E-Mail Removed) wrote:
> >
> > I have come across an unexpected problem. I thought that the array
> > asSplit() in fnFruitArray would remember its contents between calls
> > but it does not. I must have missed something elementary here - is
> > this really how VBA is supposed to work?
> >
> > Thanks,
> > Ture Magnusson
> >
> > Sub Test()
> > Dim a As Variant
> >
> > 'The first call works OK
> > a = fnFruitArray
> > MsgBox a(0)
> >
> > 'But the second call doesn't return an initialised array
> > a = fnFruitArray
> > MsgBox a(0)
> > End Sub
> >
> > Function fnFruitArray() As String()
> > Static bBeenHere As Boolean
> > Static asSplit() As String
> >
> > If bBeenHere = False Then
> > asSplit = Split("apple,orange,banana", ",")
> > bBeenHere = True
> > End If
> >
> > fnFruitArray = asSplit
> > End Function
>
> --
>
> Dave Peterson
--
Dave Peterson