?
Solved

Dynamic Arrays

Posted on 1998-05-27
9
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
Suggested Courses

770 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