-
May 8th, 2019, 03:28 PM
#1
Thread Starter
Fanatic Member
Clearing an array
I have a very large array. Is there an easy way to clear every element without going through a For...Next loop
Here's how I'm doing it now
Code:
'
'
Dim MyArray(0 To 140, 0 To 140) As Boolean
'
'
' Then later in some sub
'
For X = 0 To 140
For Y = 0 To 140
MyAreray(X, Y) = False
Next Y
Next X
'
'
Last edited by Code Dummy; May 8th, 2019 at 03:48 PM.
-
May 8th, 2019, 04:15 PM
#2
Re: Clearing an array
Make it dynamic and erase it.
EDIT1: Example:
Code:
Option Explicit
Private Sub Form_Load()
Dim MyArray() As Boolean
ReDim MyArray(0 To 140, 0 To 140)
'
'
' Then later in some sub
'
Erase MyArray
'
'
End Sub
EDIT2: And then immediately ReDim it again if you want to keep using it.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
May 8th, 2019, 04:20 PM
#3
Lively Member
Re: Clearing an array
Hi,
Just ReDim the array to the same size it is, without using the "Preserve" option.
You will need to initially declare the array without dimension(s) values, then ReDim it before first use.
e.g
Code:
Option Explicit
Dim a() As Integer ' Dynamic array, dimension not fixed
Private Sub cmdCommand1_Click()
ReDim a(3)
a(0) = 0
a(1) = 1
a(2) = 2
a(3) = 3
Debug.Print a(0), a(1), a(2), a(3) ' displays 0, 1, 2, 3
ReDim Preserve a(3)
Debug.Print a(0), a(1), a(2), a(3) ' displays 0, 1, 2, 3
ReDim a(3)
Debug.Print a(0), a(1), a(2), a(3) ' displays 0, 0, 0 ,0
End Sub
-
May 8th, 2019, 04:32 PM
#4
Re: Clearing an array
MikeSW17, good point.
A ReDim (without Preserve) will do an implicit Erase, and then re-initialize all the values to zero (False, in the case of a Boolean).
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
May 9th, 2019, 06:06 PM
#5
Thread Starter
Fanatic Member
Re: Clearing an array
Thanks, guys, That's like cool. Now I have another question. If I have two arrays the same size can I copy all the elements of one array to the other array without using a For...Next
-
May 9th, 2019, 06:16 PM
#6
Re: Clearing an array
Sure ... if the arrays are dynamic (and of the same type), you can assign (Let) them just like variables. For instance:
Code:
Option Explicit
Private Sub Form_Load()
Dim a1() As Long
Dim a2() As Long
ReDim a1(100)
a2 = a1 ' Array assignment. Even if a1 had data, it would be copied too.
End Sub
Enjoy,
Elroy
EDIT1: We've been having recent SafeArray discussions in other threads. Basically, an initialized (ReDim'med) array has a SafeArray structure and also a memory area for its data. When we do these array assignments, both the SafeArray structure and the data's memory get copied into the receiving array. Whatever was in the receiving array gets wiped out (i.e., an implicit Erase is executed).
Last edited by Elroy; May 9th, 2019 at 06:21 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
May 9th, 2019, 06:20 PM
#7
Re: Clearing an array
I like to have the index in a separate variable, like
Dim myList() as AnyContainer
Dim Lists%
when adding, I do
Lists = Lists + 1
Redim Preserve myList(1 to Lists)
and when removing I do:
if index < lists then
for i = index to lists - 1
myList(i) = myList(i + 1)
next i
end if
lists = lists - 1
if lists = 0 then Erase myList else ReDim Preserve Lists(1 to Lists)
to copy the entire array, u could do
Private Type AnyContainer
MyArray() as integer
End type
Dim myArray as AnyContainer
Dim anotherArray as AnyContainer
anotherArray = myArray
-
May 9th, 2019, 08:10 PM
#8
Re: Clearing an array
You really should not increase an array dimension by one using redim preserve. That is about the slowest possible method as it has to create a copy of the array each time you call it.
Much better to make the array bigger than you need and add chunks to it as needed. For example calling redim preserve and adding 10 elements every tenth time would be ~10 times faster than calling it every time and adding 1.
-
May 9th, 2019, 10:03 PM
#9
Re: Clearing an array
usually we are changing the Dim by user, meaning, the user is adding or removing an item.
its not a inside loop procedure where u increase and decrease an array hundreds of times every second.
if that was the case, we would create something completely different where we would not redim or erase at all.
the reason Im using this method is to save memory and make it easy to add and remove to the array.
the structure is favorable in my programs since im using 1 to x and when 0 i erase the entire array, without the need to check if the array is null or not.
Last edited by baka; May 9th, 2019 at 10:08 PM.
-
May 9th, 2019, 11:46 PM
#10
Re: Clearing an array
Originally Posted by baka
usually we are changing the Dim by user, meaning, the user is adding or removing an item.
its not a inside loop procedure where u increase and decrease an array hundreds of times every second.
if that was the case, we would create something completely different where we would not redim or erase at all.
the reason Im using this method is to save memory and make it easy to add and remove to the array.
the structure is favorable in my programs since im using 1 to x and when 0 i erase the entire array, without the need to check if the array is null or not.
When you need to redim an array several times it is better to add for example 100 elements each time (or 1000), and at the end when you already know the final number of elements, then make the final redim preserve with that number.
Before adding each element you need to check whether it is needed to redim the array. In cases when there will be many elements to add, I store the UBound of the array in a variable and the actual index in another variable, and before adding any element I check:
Code:
If CurrentIndex > UboundIndex then
UboundIndex = UboundIndex + 100
Redim Preserve MyArray (UboundIndex)
End If
If you can know the final number from the start, better. Just one redim preserve is needed then.
If you don't know for sure, but you can figure the number, then redim preserve to that supposed number + 100, and cheking each time if you need to add 100 more. And redim preserve to the actual final number at the end. That's what I do.
-
May 10th, 2019, 05:33 PM
#11
Re: Clearing an array
Another way to "clear" such an array:
Code:
Option Explicit
Private Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" ( _
ByRef Dest As Any, _
ByVal Length As Long)
Private Sub Form_Load()
Dim Truths(0 To 140, 0 To 140) As Boolean
Dim I As Long
Dim J As Long
I = Int(Rnd() * 141)
J = Int(Rnd() * 141)
Truths(I, J) = True
MsgBox "I = " & CStr(I) & vbNewLine _
& "J = " & CStr(J) & vbNewLine _
& "Truths(I, J) = " & CStr(Truths(I, J)), _
, _
"Before"
ZeroMemory Truths(0, 0), 141& * 141& * LenB(Truths(0, 0))
MsgBox "I = " & CStr(I) & vbNewLine _
& "J = " & CStr(J) & vbNewLine _
& "Truths(I, J) = " & CStr(Truths(I, J)), _
, _
"After"
Unload Me
End Sub
-
May 10th, 2019, 05:40 PM
#12
Re: Clearing an array
Originally Posted by dilettante
Another way to "clear" such an array:
That'll certainly work with the example you've provided. However, we've got to be careful if it's a String, or Variant, or Object (of any kind, late or early bound) array. It would be trivial to program a massive memory leak using that approach.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
May 10th, 2019, 05:51 PM
#13
Re: Clearing an array
I agree. It is certainly true and important to point out.
But if we have to assume people are that clueless then we shouldn't be posting about any API calls without a disclaimer like the ones Microsoft always made about modifying the registry.
The "example I provided" was the example that the question asked about though.
-
May 10th, 2019, 07:18 PM
#14
Fanatic Member
Re: Clearing an array
Re: Elroy's Post #6.
An array assignment (a2 = a1) will not work if one is using VB5.
The receiving array (a2 in this case) must be explicitly dimensioned or redimensioned first.
-
May 10th, 2019, 10:15 PM
#15
Re: Clearing an array
Originally Posted by vb6forever
Re: Elroy's Post #6.
An array assignment (a2 = a1) will not work if one is using VB5.
The receiving array (a2 in this case) must be explicitly dimensioned or redimensioned first.
Yeah, I pretty much assume that everyone using non-.NET VB is using VB6. I plugged along through the different versions (stopping at VB6), and never found any significant problems with backwards compatibility through VB6, so it's always been somewhat unclear to me why anyone would still use VB5 or earlier ... but I know they do.
Take Care,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
May 11th, 2019, 10:30 AM
#16
Fanatic Member
Re: Clearing an array
Elroy:
it's always been somewhat unclear to me why anyone would still use VB5 or earlier
FWIW in my case:
When VB6 came out I was using VB5 and other than a few (about 5) VB6 functions -- which there
are VB5 replacement for - I couldn't see upgrading. Time went on, and VB5 still met all my needs and then VB6 was Not available unless one had a MSDN subscription. So got locked in to VB5. The array assignment and the need to pass an array back as a parameter rather than at the end of a function are for the most part the only other differences I can recall right now.
-
May 11th, 2019, 10:42 AM
#17
Re: Clearing an array
Originally Posted by vb6forever
When VB6 came out I was using VB5 and other than a few (about 5) VB6 functions -- which there
are VB5 replacement for - I couldn't see upgrading. Time went on, and VB5 still met all my needs and then VB6 was Not available unless one had a MSDN subscription. So got locked in to VB5. The array assignment and the need to pass an array back as a parameter rather than at the end of a function are for the most part the only other differences I can recall right now.
Yeah, I guess "getting a copy of VB6" is a bit of an issue. Just as an FYI, it can still be "had" from eBay for $200 easily. And, if you shop (and keep an eye), you can catch copies for <$100. The last copy I bought, I got for $69. I just spotted it and thought it wouldn't hurt to have an extra copy on my shelf.
Regarding new features, I can't imaging not being able to:
- Have my functions return arrays.
- Using my UDTs in BAS modules as arguments, and with Friend in Class modules.
- Use InstrRev
- Use Split and Join
- Use Replace and/or Filter
There are a few others, but those are the ones I'd probably miss most (along with SafeArray assignment).
Take Care,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
May 11th, 2019, 02:21 PM
#18
Re: Clearing an array
[ZeroMemory-API for cleanup]
Originally Posted by Elroy
That'll certainly work with the example you've provided. However, we've got to be careful if it's a String, or Variant, or Object (of any kind, late or early bound) array. It would be trivial to program a massive memory leak using that approach.
Well, for fixed Arrays as in diles example,
the Erase statement will do the same thing (zeroing content - but leaving the dimensions intact).
The advantage of this built-in cleanup-method is, that your above concerns are taken into account
(so - also on fixed arrays, there will be a "deep-cleanup" of Variants, Strings and Objects).
Here diles fixed-array-example again - with Erase instead of ZeroMemory:
Code:
Option Explicit
Private Sub Form_Load()
Dim Truths(0 To 140, 0 To 140) As Boolean
Dim I As Long
Dim J As Long
I = Int(Rnd() * 141)
J = Int(Rnd() * 141)
Truths(I, J) = True
MsgBox "I = " & CStr(I) & vbNewLine _
& "J = " & CStr(J) & vbNewLine _
& "Truths(I, J) = " & CStr(Truths(I, J)), , "Before"
Erase Truths
MsgBox "I = " & CStr(I) & vbNewLine _
& "J = " & CStr(J) & vbNewLine _
& "Truths(I, J) = " & CStr(Truths(I, J)), , "After"
Unload Me
End Sub
Olaf
-
May 11th, 2019, 11:08 PM
#19
Re: Clearing an array
Good point. This is the way to go for fixed arrays and, with rare exceptions, all arrays.
About the only reason to use ZeroMemory might be for dynamic arrays of simple scalar types where you want to avoid the need to reallocate memory. Even then the performance gain won't matter in most programs anyway.
-
May 12th, 2019, 10:40 AM
#20
Fanatic Member
Re: Clearing an array
Elroy re:
Regarding new features, I can't imaging not being able to:
>>InstrRev, Split and Join, Replace and/or Filter
These are some of the 5 I refer to. I have VB5 implementations that are supposedly faster (no way to test) than Vb6.
In fact Microsoft put them out (Q), but I'm using other implementations.
>> Using my UDTs in BAS modules as arguments, and with Friend in Class modules.
Can pass UDT as a parameter and also return from a function (why a UDT and Not an array beyond me).
Never tried Friend in a Class module ??
>>Have my functions return arrays.
Can be returned, but passed as a parameter, NOT at the end of the function.
This is the one I miss most -- strictly from common logic since functions return some value.
For whatever reason treated arrays like Sub or Void.
=================
Since I lost the sight in my left eye in 2016 (choroidal melanoma), just kinda going along
with what I got. But thanks for the suggestion.
Last edited by vb6forever; May 12th, 2019 at 10:48 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|