Solved

Dynamic Arrays

Posted on 1998-05-27
9
268 Views
Last Modified: 2010-08-05
Simple question.  When I initially declare a dynamic array (for example, Dim Array() as User-Defined Type) will it's length automatically be set to 0, assuming Option Base 0?  If so, how do I check and confirm that the array is empty?
Thanks in advance, John
0
Comment
Question by:hozempa
  • 4
  • 3
  • 2
9 Comments
 
LVL 3

Accepted Solution

by:
fguerreiro_inix earned 0 total points
ID: 1461790
Check for the value
UDTField1=first field in UDT(if string data type)
if Array(0).UDTField1="" then
  msgbox "is empty"
else
  msgbox "is not empty"
endif
0
 
LVL 3

Expert Comment

by:fguerreiro_inix
ID: 1461791
if first field is numeric

if Array(0).UDTField1=0 then
  msgbox "is empty"
else
  msgbox "is not empty"
endif

0
 
LVL 6

Expert Comment

by:clifABB
ID: 1461792
fguerreiro_inix:

Your answer has two problems.
1. The array may not be initialized.  If this is the case, you will get a subscript out of range error.
2. The value of Array(0).UDTField1 just may be 0 (or "") as a valid entry.

hozempa:
The only way is to try to read the array and if the err,number = 9 then it is un-initialized.

Here is an example which will show "Is empty" the first time and "Is not empty" the second time.

  Dim MyArray() As tUDT
  Dim nDummy As Integer
 
  On Error Resume Next
  nDummy = MyArray(LBound(MyArray)).UDTField1
  If Err.Number = 9 Then
    MsgBox "Is empty"
  Else
    MsgBox "Is not empty"
  End If
  Err.Clear
  ReDim MyArray(1)
  nDummy = MyArray(LBound(MyArray)).UDTField1
  If Err.Number = 9 Then
    MsgBox "is empty"
  Else
    MsgBox "is not empty"
  End If

0
 
LVL 6

Expert Comment

by:clifABB
ID: 1461793
To (slightly) improve on my above example, declare nDummy as a variant.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:hozempa
ID: 1461794
Thank you for the information.  Still, I am curious to know if the statement 'Dim Array() As User-Defined Type' creates an element of 1, Array(0).  I assume that it doesn't, can anyone verify this?
0
 
LVL 3

Expert Comment

by:fguerreiro_inix
ID: 1461795
ClifABB, you're right

But after declaring the array, if
Redim Array(0) as UDT
as been made, the array has been initialized and I don't
think that all fields in the UDT should be empty at a time,
only if hozempa wants so.
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1461796
hozempa:
No, the statement Dim Array() as Anything doesn't create any elements.  This is termed 'Uninitialized'.  You can test this with the code:
Dim MyArray As tUserDefinedType
Debug.Print MyArray(0).Field1

Which will give you an array out of bounds error.
0
 
LVL 3

Expert Comment

by:fguerreiro_inix
ID: 1461797
Instead of declaring like this:
Dim MyArray() As tUserDefinedType

You could declare:
ReDim MyArray(0) As tUserDefinedType
and the array will be at once declared and initialized.

Hope this helps

0
 
LVL 1

Author Comment

by:hozempa
ID: 1461798
Thank you fguerreiro_inix and clifABB, both of you deserve the points. I'll address a question to you clifABB and you can pick up the 5 points.  Thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now