Help determining if object is loaded | |
Sion | Hey fellow coders, I'm having a little problem that someone may be able to help me with. In my application I have a control array that is created (and modified) at run time. However, I also unload some of the controls in the array at runtime so I'll like a way to determine if the object is actually loaded or not. I have a similiar problem with determining whether a collection item is existing or not. To handle that problem I've so far used a work-around (read: hack) that looks like this: [code] Private Function IsColItemValid(Col As Collection, Key As Variant) As Boolean On Error GoTo ItemIsInvalid Dim sTest As String sTest = Col.Item(Key) 'No error occured which means that an item exists with the given key. The item is valid. IsColItemValid = True Exit Function ItemIsInvalid: 'Couldn't find an item with the given key. The item is invalid. IsColItemValid = False End Function [/code] I would make such a error-based function to test if a control in a control array is loaded, but there has got to be a more elegant way...? Thanks in advance. |
Eric Coleman | For collections, the Error method is the only way. The Dictionary object has an "Exists" method to test if an item actually exists or not. For objects and controls, I use the ObjPtr function. [code] If ObjPtr(MyControl(ndx)) <> 0 then Debug.Print "Control is loaded" Else Debug.Print "Control does not exist" End if [/code] You can't test to see if the pointer to the object is > 0 because a pointer can be negative as well. A pointer value of 0 means that the variable doesn't point to anything. |
Sion | Eric, that method sounds like the right one in this case. I've been looking at some of VB build-in function for something like this, but it's apparently undocumented (and therefore unsupported).
However, I can't get it to work. I don't know what I'm doing wrong. I've made a small testing program - please take a look at it and see if you can spot any errors.
Thanks for you quick and precise answer, by the way, and sorry I've taken so long to reply. The test program is attatched to this post.
Download Attachment: [url="http://www.vbgamer.com/msgboard/uploaded/Sion/200479131727_Control_Array_Test.zip"][img]icon_paperclip.gif[/img]Control_Array_Test.zip[/url] 2 KB |
Sion | Eric, can you tell me what I've done wrong, if anything? |
Eric Coleman | Hmm, looks like objPtr only works for classes.
I guess you'll just have to use a error check to find out if the control is loaded or not. The control array looks more like a collection than an array, and just like collections there is no way to test if an element exists or not.
I made some modifications to the program. There is a function that returns true or false depending on if the control is loaded or not. There is also some commented code that shows the first 400 bytes behind the ObjPtr value. You can compare the returned values before and after you remove a button, and one thing of note is that the "left" property value is clearly visible in the data, and the first 4 bytes change when the control is unloaded. I suppose the "unload" command doesn't really unload the control.
There's also a For Each loop that demonstrates a way to iterate through all the items as though it were a collection.
Download Attachment: [url="http://www.vbgamer.com/msgboard/uploaded/Eric Coleman/200479173859_Control_Array_Test.zip"][img]icon_paperclip.gif[/img]Control_Array_Test.zip[/url] 2.5 KB |
Sion | Ok, not quite the as elegant as I was hoping for, but hey - it get the job done. I don't suppose going through the internal error handling procedure slows down the process too much. I'll use the error handling method to perform the test - it seems like the only way. Well, anyway now I can get some coding done and get back to this issue if any new techniques surfaces. Thanks for taking the time to help me Eric! :) |
Sion | I experinced some strange problems with my class, and found that it was caused by your function. If works great with a control array of command buttons, but dosn't work with a control array of winsock components. It's probably due to winsock not having the same properties as command buttons. The following code have been tested with both command button and winsock control arrays and works... Hopefully it also works in my class! [code] Function IsControlLoaded(Ctrl As Object, iIndex As Integer) As Boolean On Error GoTo ControlIsMissing IsControlLoaded = True If Ctrl(iIndex) Then 'This raises an error if the control dosn't exist 'Code here is never executed - the error handler has taken over End If Exit Function ControlIsMissing: If Err.Number = 340 Then IsControlLoaded = False 'Error "Control index is not loaded" has err. num. 340 End Function [/code] |