|
-
Jun 20th, 2002, 12:16 AM
#1
Thread Starter
Junior Member
How to find out if control array element exists
I've got a program that creates and deletes control array elements at runtime. I'm relatively new to VB, so I don't know how to check to see if an element exists. I want something along this line though
If controlArray(x) does not exist
Load controlArray(x)
else
Unload controlArray(x)
I tried isEmpty and isNull, but those seemed to return false regardless of whether the control array element was set or not
-
Jun 20th, 2002, 01:15 AM
#2
Hyperactive Member
Check the UBound(YourArrayName), this will return the size of the array. But if you want to check the content of the array itself you should loop on it.
Bear in mind though that UBound of a dynamic array that has not yet been resized would raise a runtime error. So maybe before you check for UBound you could check for IsArray(YourArrayName).
-
Jun 20th, 2002, 01:20 AM
#3
Thread Starter
Junior Member
Good stuff to know, but I'm not sure it applies to my situation. The size of the array seems to be irrelevant to me, and I know that it's always going to be an array, since the program starts with ControlArray(0) already in the form..
I want to be able to pick an arbitrary number and see if that index is an element in the control array. If it's not, load it into the form. if it's already loaded, do nothing.
-
Aug 28th, 2002, 02:13 PM
#4
New Member
List of indexes not continuous
The Ubound technique only works where you have a control array that has no gaps in. I need to have gaps in my control array which means that certain elements do not exist. for instance, my control array has 118 textboxes but element '2' does not exist. is there any way to check if the actual element exists?
Please Help ASAP.
-
Aug 28th, 2002, 02:20 PM
#5
why not just error trap error '340'?
-
Aug 28th, 2002, 02:41 PM
#6
I second kleinma use error trapping, you can even right a function to test it if you want:
VB Code:
Public Function IsInArray(CtrlArray as Variant,Index as integer) as boolean
On Error Resume Next
Dim x As String
x=CtrlArray(Index).Name
If Err.Number=0 Then IsInArray=TRUE
End Function
-
Aug 28th, 2002, 02:43 PM
#7
Originally posted by Edneeis
I second kleinma use error trapping, you can even right a function to test it if you want:
VB Code:
Public Function IsInArray(CtrlArray as Variant,Index as integer) as boolean
On Error Resume Next
Dim x As String
x=CtrlArray(Index).Name
If Err.Number=0 Then IsInArray=TRUE
End Function
using error trapping is greatly overlooked... people don't like to do it because it has the word "ERROR" in it.. but it is actually sometimes the best way to code things...
-
Aug 28th, 2002, 03:20 PM
#8
I agree, they need to focus on the 'trapping' part instead of the 'error' part or something.
-
Aug 31st, 2002, 08:02 AM
#9
New Member
Cheers guys, that works fine now.
I did try trapping but for some reason it failed before.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|