Solved

Dynamic Arrays

Posted on 1998-05-27
9
271 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 6

Expert Comment

by:clifABB
ID: 1461793
To (slightly) improve on my above example, declare nDummy as a variant.
0
 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 67
VB6 - Convert HH:MM into Decimal 8 63
vbModal 12 57
Remove Exif Orientation before adding image to db 8 11
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

820 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