Erase Statement

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)
 
F

Fumei2 via OfficeKB.com

Hi Greg. Take a look at:

StrFixArray(0) = "11111"
StrFixArray(1) = "2"
StrFixArray(2) = "33333"
StrFixArray(3) = "444444444"

Dim StrFixArray(0 To 3) As String * 5

The "fixed-string" array is "fixed" as 5 characters. If the value is less
than five characters - say "2" - it completes the count by appending spaces.

StrFixArray(1) = "2 "

If it is given a value of more than five characters, it trims the trailing
excess off.

StrFixArray(3) = "44444"

So your statement::

Erase StrFixArray 'Each element set to 0.

is not quite correct. It is not set to 0. It is set to:

00000

That is, ASCII 0, or...if you prefer:

Null, Null, Null, Null, Null

Check the ASCII values of two of the items.

StrFixArray(0) = "11111"

Asc(Right(strFixArray(0), 1)) = 49 (or 1)

AFTER the Erase it equals 0 (or Null)


StrFixArray(1) = "2 "

Asc(Right(strFixArray(1), 1)) = 32 (or Space)

AFTER the Erase it equals 0 (or Null)

After you do an Erase, check the Len of the value.

Debug.Pring Len(StrFixArray(1)) - shows as 5
Erase StrFixArray 'Each element set to 0.
Debug.Pring Len(StrFixArray(1)) - STILL shows as 5

So, no, it does not set it as a zero-length string.
 
F

Fumei2 via OfficeKB.com

You do have a point though, regarding Help. It states:

Fixed string array (fixed length) Sets each element to zero.

However.........it depends what you consider "element".

From one perspective, EACH character in a fixed-length string array is an
"element".

In which case, ASCII 00000 for a *5 defined string array is perfectly
sensible.


I remain unconvinced. I think Help should have stated:

Fixed string array (fixed length) Sets each character element to ASCII zero
(Null).
Hi Greg. Take a look at:

StrFixArray(0) = "11111"
StrFixArray(1) = "2"
StrFixArray(2) = "33333"
StrFixArray(3) = "444444444"

Dim StrFixArray(0 To 3) As String * 5

The "fixed-string" array is "fixed" as 5 characters. If the value is less
than five characters - say "2" - it completes the count by appending spaces.

StrFixArray(1) = "2 "

If it is given a value of more than five characters, it trims the trailing
excess off.

StrFixArray(3) = "44444"

So your statement::

Erase StrFixArray 'Each element set to 0.

is not quite correct. It is not set to 0. It is set to:

00000

That is, ASCII 0, or...if you prefer:

Null, Null, Null, Null, Null

Check the ASCII values of two of the items.

StrFixArray(0) = "11111"

Asc(Right(strFixArray(0), 1)) = 49 (or 1)

AFTER the Erase it equals 0 (or Null)

StrFixArray(1) = "2 "

Asc(Right(strFixArray(1), 1)) = 32 (or Space)

AFTER the Erase it equals 0 (or Null)

After you do an Erase, check the Len of the value.

Debug.Pring Len(StrFixArray(1)) - shows as 5
Erase StrFixArray 'Each element set to 0.
Debug.Pring Len(StrFixArray(1)) - STILL shows as 5

So, no, it does not set it as a zero-length string.
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
[quoted text clipped - 56 lines]
Resume Err_ReEntry2
End Sub
 
P

Peter Jamieson

Erase sets each character of the fixed-length string to a 0 value (not a
"0" character). Or NUL, if you prefer). i.e. if you do

erase strfixArray
debug.print asc(mid(strfixarray(0),1,1))
debug.print asc(mid(strfixarray(0),2,1))

you should see

0
0

I would describe the documentation as ambiguous.

Peter Jamieson

http://tips.pjmsn.me.uk
 
G

Greg Maxey

Fumei/Peter,

Thanks for your responses. I can make sense of it now and on reflection I
probably should have tinkered with it a bit more before posting. I did
notice that when I moused over the line

MsgBox StrFixArray(0) after the erase I was seeing five empty boxes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top