Solved

Problem accessing non-sequentially indexed array elements in a Variant

Posted on 2000-03-21
3
273 Views
Last Modified: 2010-05-02
I have a control array with non-sequential indexes (i.e 3, 7, 9, 15) which is completely valid in VB (and often useful).  It is passed to a function as a Variant (apparently the only way to pass it).

As a Variant inside the function I can't figure out how to loop through the elements.

With a plain old array you can use LBound and UBound and ignore the invalid index errors that occur along the way with an On Error statement (perhaps there is a better way).  But with the Variant LBound and UBound don't work.

any ideas?
0
Comment
Question by:jleinbach
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
mcrider earned 150 total points
ID: 2640717
Here you go...

Dim Element As Variant

Load Label1(5)
Load Label1(6)
Load Label1(11)
For Each Element In Label1
    Debug.Print Element.Index
Next


Cheers!
0
 

Author Comment

by:jleinbach
ID: 2640766
This solves the problem.  Thank you.

As a follow up: Is it possible to pass the control array to the function any way *other than* as a Variant?

Also, does it make sense, if I have a control array that does not need to be passed, to assign it to a Variant anyway, just so I can use the For Each construct rather than looping from LBound to UBound and ignoring the 'Index Does Not Exist' errors along the way?

Seems to be preferable, but can you provide any addition insight.

Thanks again for the help.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2640904
Thanks for the points! Glad I could help!

BTW, are you talking about something like this:

   Function TestIt(ObjName As Object)
       Dim Element As Variant
       For Each Element In ObjName
           Debug.Print Element.Index
       Next
   End Function

   Private Sub Command1_Click()
       Load Label1(5)
       Load Label1(6)
       Load Label1(11)
       TestIt Label1
   End Sub


Cheers!®©
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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 (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

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