|
-
Jul 13th, 2010, 01:32 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Uninitialized Arrays
Awhile ago I came across a neat trick to detect uninitialized arrays, e.g.
Code:
Dim foo() as string
If XXX = -1 Then Debug.Print "Array is uninitialized."
I can't remember what XXX is, I apparently didn't make a note of it in my Google Notebook of VBA tricks, and I can't remember which of the countless Excel apps I used this trick in.
It was really short, neat but unintuitive. I remember that whatever the test was, the value was supposed to be -1. I could always error trap a UBound call, but I'd like to know what the trick was.
Does anyone know?
Thanks!
Pete
-
Jul 13th, 2010, 01:55 PM
#2
Re: Uninitialized Arrays
This is one way to check... the API way...
Code:
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" _
(ByRef saArray() As Any) As Long
Sub Sample()
Dim foo() As String
If SafeArrayGetDim(foo) = 0 Then MsgBox "Array is uninitialized."
End Sub
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jul 13th, 2010, 02:09 PM
#3
Re: Uninitialized Arrays
Or maybe with the help of a helper function like this:
vb Code:
Sub Sample() Dim foo() As String If GetUbound(foo) = -1 Then Debug.Print "Array is uninitialized." End Sub Function GetUbound(arr) As Integer On Error Resume Next GetUbound = -1 GetUbound = UBound(arr) End Function
-
Jul 13th, 2010, 02:27 PM
#4
Thread Starter
Addicted Member
Re: Uninitialized Arrays
Awesome trick! That's going right into my Google notebook!
-
Jul 14th, 2010, 04:39 AM
#5
Re: Uninitialized Arrays
 Originally Posted by Pradeep1210
Or maybe with the help of a helper function like this:
vb Code:
Sub Sample()
Dim foo() As String
If GetUbound(foo) = -1 Then Debug.Print "Array is uninitialized."
End Sub
Function GetUbound(arr) As Integer
On Error Resume Next
GetUbound = -1
GetUbound = UBound(arr)
End Function
The above trick has a flaw: Although not many people use it but it is legal for an array to have both Lbound() and Ubound() negative.
You can declare or redim like this:
Code:
Dim foo(-5 to -1) as String
or
Code:
Dim foo() as String
'...
Redim foo(-5 to -1)
In these cases, GetUbound(foo) will return -1 even foo has been initialized.
Instead of GetUbound(), perhaps this is better:
Code:
Function IsArrayInit(arr) as Boolean
Dim n as Integer
On Error Resume Next
n = Ubound(arr)
IsArrayInit = (Err = 0)
End Function
The XXX in the original question is:
Code:
If Not Not foo = -1 Then
However, people said this method may cause some problem in VB6-IDE and Debug.Assert need to be used to fix.
Tags for this Thread
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
|