G
Greg Maxey
I have been toying with the Erase statement. The VBA Help states that the
Erase statement sets each element of a fixed-string array to 0. From the
testing that I have done shown below, it appears that it sets each element
of a fixed-string array to a zero-length string. Am I misunderstanding
"fixed-string" array? Is this the expected behaviour and the Help discussion
is wrong, or is this a bug?
Sub EraseStatement()
Dim i As Long
'Declare array variables.
Dim NumArray(0 To 3) As Integer 'Integer array.
Dim StrVarArray(0 To 3) As String 'Variable-string array.
Dim StrFixArray(0 To 3) As String * 5 'Fixed-string array.
Dim VarArray As Variant 'Variant array.
Dim DynamicArray() As String 'Dynamic array.
ReDim DynamicArray(0 To 3) 'Allocate storage space.
For i = 1 To 4
NumArray(i - 1) = i
Next i
MsgBox NumArray(3)
Erase NumArray 'Each element set to 0.
MsgBox NumArray(3)
StrVarArray(0) = "One"
StrVarArray(1) = "Two"
StrVarArray(2) = "Three"
MsgBox StrVarArray(1)
Erase StrVarArray 'Each element set to zero-length string ("").
MsgBox StrVarArray(1)
StrFixArray(0) = "11111"
StrFixArray(1) = "22222"
StrFixArray(2) = "33333"
MsgBox StrFixArray(0)
Erase StrFixArray 'Each element set to 0.
MsgBox StrFixArray(0)
VarArray = Array("One", "Two", "Three")
MsgBox VarArray(1)
Erase VarArray 'Each element set to Empty.
On Error GoTo Err_Handler1
MsgBox VarArray(1)
Err_ReEntry1:
On Error GoTo 0
DynamicArray(0) = "aaaaa"
DynamicArray(1) = "bbbbb"
DynamicArray(2) = "ccccc"
MsgBox DynamicArray(2)
Erase DynamicArray 'Free memory used by array.
On Error GoTo Err_Handler2
Err_ReEntry2:
MsgBox DynamicArray(2)
Exit Sub
Err_Handler1:
MsgBox "The array has been erased and elements set to empty"
Resume Err_ReEntry1
Err_Handler2:
ReDim DynamicArray(0 To 2)
DynamicArray(0) = "xxxxx"
DynamicArray(1) = "yyyyy"
DynamicArray(2) = "zzzzz"
Resume Err_ReEntry2
End Sub
--
Greg Maxey
See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.
Arrogance is a weed that grows mostly on a dunghill (Arabic proverb)
Erase statement sets each element of a fixed-string array to 0. From the
testing that I have done shown below, it appears that it sets each element
of a fixed-string array to a zero-length string. Am I misunderstanding
"fixed-string" array? Is this the expected behaviour and the Help discussion
is wrong, or is this a bug?
Sub EraseStatement()
Dim i As Long
'Declare array variables.
Dim NumArray(0 To 3) As Integer 'Integer array.
Dim StrVarArray(0 To 3) As String 'Variable-string array.
Dim StrFixArray(0 To 3) As String * 5 'Fixed-string array.
Dim VarArray As Variant 'Variant array.
Dim DynamicArray() As String 'Dynamic array.
ReDim DynamicArray(0 To 3) 'Allocate storage space.
For i = 1 To 4
NumArray(i - 1) = i
Next i
MsgBox NumArray(3)
Erase NumArray 'Each element set to 0.
MsgBox NumArray(3)
StrVarArray(0) = "One"
StrVarArray(1) = "Two"
StrVarArray(2) = "Three"
MsgBox StrVarArray(1)
Erase StrVarArray 'Each element set to zero-length string ("").
MsgBox StrVarArray(1)
StrFixArray(0) = "11111"
StrFixArray(1) = "22222"
StrFixArray(2) = "33333"
MsgBox StrFixArray(0)
Erase StrFixArray 'Each element set to 0.
MsgBox StrFixArray(0)
VarArray = Array("One", "Two", "Three")
MsgBox VarArray(1)
Erase VarArray 'Each element set to Empty.
On Error GoTo Err_Handler1
MsgBox VarArray(1)
Err_ReEntry1:
On Error GoTo 0
DynamicArray(0) = "aaaaa"
DynamicArray(1) = "bbbbb"
DynamicArray(2) = "ccccc"
MsgBox DynamicArray(2)
Erase DynamicArray 'Free memory used by array.
On Error GoTo Err_Handler2
Err_ReEntry2:
MsgBox DynamicArray(2)
Exit Sub
Err_Handler1:
MsgBox "The array has been erased and elements set to empty"
Resume Err_ReEntry1
Err_Handler2:
ReDim DynamicArray(0 To 2)
DynamicArray(0) = "xxxxx"
DynamicArray(1) = "yyyyy"
DynamicArray(2) = "zzzzz"
Resume Err_ReEntry2
End Sub
--
Greg Maxey
See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.
Arrogance is a weed that grows mostly on a dunghill (Arabic proverb)