-
[RESOLVED] Dynamic array: has it been initialized?
I'm not sure I've chosen a proper title for this thread, anyway, I want to test if a dynamic array has already been ReDim-ed. In other words, what should I replace '???' by?
Code:
Dim MyArray() As Integer
ReDim MyArray(10)
If ??? Then
'Do this
Else
'Do that
End If
??? means that the bold statement has been executed.
-
Re: Dynamic array: has it been initialized?
I'm sure there's a better way, but try this:
Code:
Dim MyArray() As Integer
ReDim MyArray(10)
On Error Resume Next
MsgBox UBound(MyArray)
If Err.Number <> 0 Then
MsgBox Err.Description
End If
Then comment out the "ReDim" and run it again.
-
Re: Dynamic array: has it been initialized?
Quote:
Originally Posted by
vbfbryce
I'm sure there's a better way, but try this:
Code:
Dim MyArray() As Integer
ReDim MyArray(10)
On Error Resume Next
MsgBox UBound(MyArray)
If Err.Number <> 0 Then
MsgBox Err.Description
End If
Then comment out the "ReDim" and run it again.
Trapping the error, yes, there's always that, but I was thinking of something more elegant, you know, IsNull, IsNothing, IsEmpty, IsWhatNot or similar.
-
Re: Dynamic array: has it been initialized?
There's nothing (no pun intended) built in.
-
Re: Dynamic array: has it been initialized?
If 0 isn't valid in MyArray(0) then you can do this
Code:
If MyArray(LBound(MyArray)) = 0 Then
MsgBox "empty"
End If
-
Re: Dynamic array: has it been initialized?
I've used this, I've never ran into a problem with it. The Assert is there to prevent some buggy things from happening.
Code:
Function CheckArray(MyInt() As Integer) As Boolean
Dim i As Long
i = Not Not MyInt
Debug.Assert App.hInstance
If Not i = 0 Then
CheckArray = True
Else
CheckArray = False
End If
End Function
Usage:
Code:
Sub TestSub()
Dim MyInt1() As Integer, MyInt2() As Integer
ReDim MyInt2(3 To 5)
MsgBox CheckArray(MyInt1) 'Return False, the Array IS NOT initialized
MsgBox CheckArray(MyInt2) ' Return True, the array IS initialized
End Sub
-
Re: Dynamic array: has it been initialized?
Would this help?
Code:
Dim MyArray() As Integer
' ReDim MyArray(10)
' Check if Array is empty
If (Not MyArray()) = -1 Then
MsgBox "Array Empty/Not Dimensioned"
Else
'Do that
End If
-
Re: Dynamic array: has it been initialized?
Quote:
Originally Posted by
MartinLiss
If 0 isn't valid in MyArray(0) then you can do this
Code:
If MyArray(LBound(MyArray)) = 0 Then
MsgBox "empty"
End If
Gives "subscript out of range" because MyArray hasn't been initialized yet.
-
Re: Dynamic array: has it been initialized?
Quote:
Originally Posted by
Teseng
I've used this, I've never ran into a problem with it. The Assert is there to prevent some buggy things from happening.
Code:
Function CheckArray(MyInt() As Integer) As Boolean
Dim i As Long
i = Not Not MyInt
Debug.Assert App.hInstance
If Not i = 0 Then
CheckArray = True
Else
CheckArray = False
End If
End Function
Usage:
Code:
Sub TestSub()
Dim MyInt1() As Integer, MyInt2() As Integer
ReDim MyInt2(3 To 5)
MsgBox CheckArray(MyInt1) 'Return False, the Array IS NOT initialized
MsgBox CheckArray(MyInt2) ' Return True, the array IS initialized
End Sub
Well, it works, but what buggy things do you mean?
-
Re: Dynamic array: has it been initialized?
Quote:
Originally Posted by
Edgemeal
Would this help?
Code:
Dim MyArray() As Integer
' ReDim MyArray(10)
' Check if Array is empty
If (Not MyArray()) = -1 Then
MsgBox "Array Empty/Not Dimensioned"
Else
'Do that
End If
Yes it does!
-
Re: Dynamic array: has it been initialized?
Seems to me that you would have to check all the integer elements of the array. Only if all were 0 would it then be safe to ReDim it unless you purposely want to reset all of the elements to 0. :ehh:
-
Re: [RESOLVED] Dynamic array: has it been initialized?
If you don't like the error trapping way (simplest and bulletproof) then try this:
Determining Array Initialization State and Dimensions
-
Re: Dynamic array: has it been initialized?
Quote:
Originally Posted by
Code Doc
Seems to me that you would have to check all the integer elements of the array. Only if all were 0 would it then be safe to ReDim it unless you purposely want to reset all of the elements to 0. :ehh:
Well, I'm not sure I follow you. The idea is, some code must be skipped if the array hasn't been yet redim-ed and given values.
-
Re: [RESOLVED] Dynamic array: has it been initialized?
A strange use of "Not Not" that Tseng mentioned in post#6
Code:
Dim MyArray() As Integer
If (Not Not MyArray) = 0 Then
MsgBox "MyArray was Not Initialized"
Else
MsgBox "MyArray was Initialized with UBound = " & UBound(MyArray)
End If
ReDim MyArray(10)
If (Not Not MyArray) = 0 Then
MsgBox "MyArray was Not Initialized"
Else
MsgBox "MyArray was Initialized with UBound = " & UBound(MyArray)
End If
-
Re: [RESOLVED] Dynamic array: has it been initialized?
Quote:
Originally Posted by
RhinoBull
Thank you, I bookmark the site for possible future reference. However I like edgemels's solution in just one statement.
Trapping the error is a matter of taste, offhand I sort of dislike it as it looks like you're mending some faulty code, but I imagine it's just a matter of getting used to it.
-
Re: [RESOLVED] Dynamic array: has it been initialized?
Quote:
Originally Posted by
krtxmrtz
Trapping the error is a matter of taste, offhand I sort of dislike it as it looks like you're mending some faulty code, but I imagine it's just a matter of getting used to it.
Nope, not in this case at least.
-
Re: [RESOLVED] Dynamic array: has it been initialized?
Quote:
Originally Posted by
anhn
A strange use of "Not Not" that Tseng mentioned in post#6...
I assume
If (Not MyArray()) = -1 Then
is the same as
If (Not Not MyArray) = 0 Then
-
Re: [RESOLVED] Dynamic array: has it been initialized?
Quote:
Originally Posted by
RhinoBull
Nope, not in this case at least.
Well, perhaps you've convinced me that error trapping is ok, still it takes one more statement (On Error Resume Next)
-
Re: [RESOLVED] Dynamic array: has it been initialized?
Have your tried API SafeArrayGetDim?
Code:
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
Private Type ProjectType
ProjectCode As String
Priority As Integer
End Type
Private Sub Form_Load()
Dim l() As Long
Debug.Print "IsArrayInitiaized l()", SafeArrayGetDim(l) > 0
ReDim l(0)
Debug.Print "IsArrayInitiaized l(0)", SafeArrayGetDim(l) > 0
Debug.Print
'
Dim s() As String
Debug.Print "IsArrayInitiaized s()", SafeArrayGetDim(s) > 0
ReDim s(0)
Debug.Print "IsArrayInitiaized s(0)", SafeArrayGetDim(s) > 0
Debug.Print
'
Dim v() As Variant
Debug.Print "IsArrayInitiaized v()", SafeArrayGetDim(v) > 0
ReDim v(0)
Debug.Print "IsArrayInitiaized v(0)", SafeArrayGetDim(v) > 0
Debug.Print
Dim p() As ProjectType
Debug.Print "IsArrayInitiaized p()", SafeArrayGetDim(p) > 0
ReDim p(0) As ProjectType
Debug.Print "IsArrayInitiaized p(0)", SafeArrayGetDim(p) > 0
End Sub
-
Re: Dynamic array: has it been initialized?
Quote:
Originally Posted by
krtxmrtz
Well, it works, but what buggy things do you mean?
For some reason, the Not Not Method, does something to the array pointer, and unless you do the assert, it can cause some wacky things, such as "i = 1 + 1" will give the error "math too complex" The assert fixes it completely, i don't understand it completely, but it seems to work fine.
-
Re: [RESOLVED] Dynamic array: has it been initialized?
Quote:
Originally Posted by
DrUnicode
Have your tried API SafeArrayGetDim?
...
I like it, :thumb:
I have used much the API function compilation at allapi
but it's no longer maintained and up to date so, this function I didn't know about.
-
Re: [RESOLVED] Dynamic array: has it been initialized?
Not Not Arrayname
This returns the address of the Safearray Descriptor used by VB to describe arrays. If an Array is uninitialised this pointer is null hence If Not Arrayname <> -1 Then initialised
I don't think this feature is there by design and there is a bug. The bug only happens in the IDE and only in the first floating point calculation following the first use of Not Arrayname. The bug can be avoided by calling any internal class after the first use of Not Arrayname
Debug.Assert App.hInstance '<-- this avoids the IDE only bug and does not compile
-
Re: [RESOLVED] Dynamic array: has it been initialized?
For an in depth dicussion on this subject just a little over a year ago on another site, see...
http://www.tek-tips.com/viewthread.cfm?qid=1495917
Hope it helps...
Good Luck
-
Re: [RESOLVED] Dynamic array: has it been initialized?
I found this thread while researching the same issue as krtxmrtz. Though this thread is resolved, I just wanted to say that all contributors made this a stimulating read. :thumb:
-
Re: [RESOLVED] Dynamic array: has it been initialized?
True,UsedTime2=0.0349 Ms--GetLbound
True,UsedTime1=0.0494 Ms--API--CopyMemory--IsArrayReDims
True,UsedTime2=0.0099 Ms--API--SafeArrayGetDim
Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal cb&)
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
Function GetLbound(array1 As Variant) As Boolean
On Error Resume Next
Dim a As Long
a = LBound(array1)
GetLbound = Err.Number = 0
End Function
Public Function IsArrayReDims(wArray As Variant) As Boolean
'?????????
Dim a As Long
If IsArray(wArray) Then
CopyMemory a, ByVal VarPtr(wArray) + 8, 4
CopyMemory a, ByVal a, 4
End If
IsArrayReDims = a <> 0
End Function
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
timeEndPeriod 1
End Sub
Function CheckArray(MyArray As Variant) As Boolean
If (Not MyArray()) = -1 Then
Else
CheckArray = True
End If
End Function