Solved

Dynamic Arrays

Posted on 1998-05-27
9
267 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

757 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

22 Experts available now in Live!

Get 1:1 Help Now