|
-
Dec 11th, 2009, 02:32 PM
#1
[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.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Dec 11th, 2009, 02:41 PM
#2
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.
-
Dec 11th, 2009, 03:15 PM
#3
Re: Dynamic array: has it been initialized?
 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.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Dec 11th, 2009, 03:21 PM
#4
Re: Dynamic array: has it been initialized?
There's nothing (no pun intended) built in.
-
Dec 11th, 2009, 03:30 PM
#5
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
-
Dec 11th, 2009, 03:45 PM
#6
Hyperactive Member
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
-
Dec 11th, 2009, 03:48 PM
#7
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
-
Dec 11th, 2009, 03:53 PM
#8
Re: Dynamic array: has it been initialized?
 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.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Dec 11th, 2009, 03:57 PM
#9
Re: Dynamic array: has it been initialized?
 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?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Dec 11th, 2009, 04:01 PM
#10
Re: Dynamic array: has it been initialized?
 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!
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Dec 11th, 2009, 04:01 PM
#11
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.
-
Dec 11th, 2009, 04:03 PM
#12
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
-
Dec 11th, 2009, 04:04 PM
#13
Re: Dynamic array: has it been initialized?
 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. 
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.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Dec 11th, 2009, 04:04 PM
#14
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
-
Dec 11th, 2009, 04:10 PM
#15
Re: [RESOLVED] Dynamic array: has it been initialized?
 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.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Dec 11th, 2009, 04:11 PM
#16
Re: [RESOLVED] Dynamic array: has it been initialized?
 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.
-
Dec 11th, 2009, 04:13 PM
#17
Re: [RESOLVED] Dynamic array: has it been initialized?
 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
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Dec 11th, 2009, 04:19 PM
#18
Re: [RESOLVED] Dynamic array: has it been initialized?
 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)
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Dec 11th, 2009, 06:43 PM
#19
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
-
Dec 11th, 2009, 07:47 PM
#20
Hyperactive Member
Re: Dynamic array: has it been initialized?
 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.
-
Dec 12th, 2009, 04:15 AM
#21
Re: [RESOLVED] Dynamic array: has it been initialized?
 Originally Posted by DrUnicode
Have your tried API SafeArrayGetDim?
...
I like it, 
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.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Dec 12th, 2009, 09:06 AM
#22
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
-
Dec 12th, 2009, 06:27 PM
#23
Frenzied Member
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
Option Explicit should not be an Option!
-
Feb 17th, 2010, 09:25 PM
#24
-
Nov 14th, 2024, 12:02 AM
#25
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
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
|