howto test for uninitialised array

  • Thread starter Thread starter Eric Frohmann
  • Start date Start date
E

Eric Frohmann

I'm trying to maintain a list of forms in my app. How can I test for an
uninitialised array?

Here's my current effort:

Module::
Public GarstOpenForms() As String

Public Sub esInitFormList(bCloseForms As Boolean)
On Error GoTo Err_esInitFormList
'init value - false mean we do not automatically close forms
GbForceFormClose = bCloseForms

If (IsNull(GarstOpenForms())) Then '<<<<<<<
' tried IsNull(GarstOpenForms) - it didn't work either
'initialise the first value
ReDim GarstOpenForms(1)
GarstOpenForms(1) = ""
End If

Exit_esInitFormList:
Exit Sub

Err_esInitFormList:
MsgBox "::esInitFormList:" & Err.Description
Resume Exit_esInitFormList
End Sub
 
You can use UBound to ask the upper bound (max dimension)
of the array.

UBound(arrayname)

Is this it?

David
 
Thanx for the response.

Nope - I get "Subscript out of range"

If (UBound(GarstOpenForms()) <= 0) Then
OR
If (UBound(GarstOpenForms) <= 0) Then

Is there no equivalent to array.isAssigned()???

I'm tempted to trap for an error, and assume that the array's not been
initialised if there is one - but this seems like a real kludge to me...
 
Hi Eric

Good question! AFAIK there is no clean way to do it - LBound and UBound
will both raise errors if the array has not yet been dimmed. Here is a
function which traps the error for you and returns a result:

Public Function IsArrayDimmed(a As Variant) As Boolean
On Error GoTo ProcErr
If IsNumeric(LBound(a)) Then IsArrayDimmed = True
ProcEnd:
Exit Function
ProcErr:
With Err
If .Number = 9 Then Resume ProcEnd
.Raise .Number, .Source, .Description, .HelpFile, .HelpContext
End With
Resume ProcEnd
End Function

--
Good Luck!
Graham Mandeno [Access MVP]
Auckland, New Zealand

Return mail address is invalid in a vain attempt to reduce spam.
Feedback is welcome at: (e-mail address removed)
Please post new questions or followups to newsgroup.
 
A string can never have a null value. The nearest equivalent is an empty
string "" You would have to check each element in the array to see if its
empty, before deciding that the array is not initialised, unless you are
certain that the first element is always used.

Something like:
dim k as long
din flag as boolean

flag = false
for k = lbound(GarstOpenForms()) to ubound(GarstOpenForms())
if GarstOpenForms(k) <> "" then
flag = true
endif
next k

You will still have to trap for errors on using the ubound and lbound
functions.


--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
Thanx. Good to know that I wasn't missing something obvious! The dents in
the wall can be repaired I think.
 
Eric
Thanx. Good to know that I wasn't missing something obvious! The dents in
the wall can be repaired I think.

Amazing what you can do with a bit of gap filler :-)

Just an afterthought - another possibility is to Dim the array with an upper
bound of zero:
Public GarstOpenForms(0) As String

It seems you are not using the zeroth element anyway. You can then test for
UBound()=0 to see if you've put anything in the array.

Incidentally, why do you want to keep an array of the names of open forms?
The Forms collection already does this for you quite nicely.

--
Good Luck!
Graham Mandeno [Access MVP]
Auckland, New Zealand

Return mail address is invalid in a vain attempt to reduce spam.
Feedback is welcome at: (e-mail address removed)
Please post new questions or followups to newsgroup.
 
Back
Top