VB icon

Small tips: validate control array index

Email
Submitted on: 2/21/2015 4:34:00 AM
By: Vesa Piittinen (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 2315
 
     

I've seen many ways to check whether a certain control index exists in a control array, where all indexes are not filled. For example, you could have a control array of labels with indexes 0, 1 & 3. I guess one of the most common ways to check for this is to use On Error, but I've always found resorting to rising errors a bad habit to do. A much better another alternative is to loop through the control array using For Each and check for the Index property, but even this becomes a bit silly if you know the index you want to check for and loop the entire control array just to check if it is there or not.

The solution: VarType()

VarType returns the variable type of default property of a control, ie. VarType(MyLabels(1)) returns 8 (vbString), from Caption property. What makes this usable for sniffing for valid index is that VarType returns 9 (vbObject) for invalid indexes! There is no control that returns an object in their default property, making this a perfectly safe method to check if an index exists or not.


 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Small tips: validate control array index
' Description:<p>I've seen many ways to check whether a certain control index exists in a control array, where all indexes are not filled. For example, you could have a control array of labels with indexes 0, 1 & 3. I guess one of the most common ways to check for this is to use <b>On Error</b>, but I've always found resorting to rising errors a bad habit to do. A much better another alternative is to loop through the control array using For Each and check for the Index property, but even this becomes a bit silly if you know the index you want to check for and loop the entire control array just to check if it is there or not.</p>
<p>The solution: <b>VarType()</b></p>
<p>VarType returns the variable type of default property of a control, ie. <b>VarType(MyLabels(1))</b> returns 8 (vbString), from Caption property. What makes this usable for sniffing for valid index is that VarType returns 9 (vbObject) for invalid indexes! There is no control that returns an object in their default property, making this a perfectly safe method to check if an index exists or not.</p>
' By: Vesa Piittinen (from psc cd)
'**************************************

' make sure you add MyLabel to your form and set Index property to 0
Option Explicit
Private Sub Form_Load()
' control array indexes are Integers
Dim I As Integer
Dim C As Label
Load MyLabel(1)
Load MyLabel(3)
' solution one: On Error... looping through all controls
On Error Resume Next
For I = MyLabel.LBound To MyLabel.UBound
' check if the index is there
MyLabel(I) = MyLabel(I)
If Err = 0 Then
' do what you need to do...
Else
Debug.Print "On Error, Invalid index: " & I
Err.Clear
End If
Next I
' that wasn't pretty...
On Error GoTo 0
' solution two: For Each for specific index, must loop through all controls
I = 2
For Each C In MyLabel
' if the index is there we exit the loop
If C.Index = I Then Exit For
Next C
' if we passed though all controls then C is now Nothing
If Not C Is Nothing Then
Debug.Print "For Each, VALID index: " & I
Else
Debug.Print "For Each, Invalid index: " & I
End If
' solution three: VarType looping through all controls
For I = MyLabel.LBound To MyLabel.UBound
If VarType(MyLabel(I)) <> vbObject Then
' VALID
Else
Debug.Print "VarType, Invalid index: " & I
End If
Next I
' solution four: VarType for specific index
I = 2
If VarType(MyLabel(I)) = vbObject Then Debug.Print "VarType, Invalid index: " & I
End Sub


Other 19 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.