Post

 Resources 

Console

Home | Profile | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 VBGamer
 VBGamer
 Help determining if object is loaded
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Sion
Warrior

Denmark
138 Posts

Posted - Jul 09 2004 :  11:42:33 AM  Show Profile  Visit Sion's Homepage  Click to see Sion's MSN Messenger address  Reply with Quote
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:

  
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
  


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.

Visit my personal blog at www.AndersNissen.com!

Eric Coleman
Gladiator

USA
811 Posts

Posted - Jul 09 2004 :  11:58:19 AM  Show Profile  Visit Eric Coleman's Homepage  Reply with Quote
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.

  
If ObjPtr(MyControl(ndx)) <> 0 then
   Debug.Print "Control is loaded"  
Else
   Debug.Print "Control does not exist"  
End if
  


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.

Go to Top of Page

Sion
Warrior

Denmark
138 Posts

Posted - Jul 09 2004 :  1:19:20 PM  Show Profile  Visit Sion's Homepage  Click to see Sion's MSN Messenger address  Reply with Quote
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: Control_Array_Test.zip
2 KB

Visit my personal blog at www.AndersNissen.com!
Go to Top of Page

Sion
Warrior

Denmark
138 Posts

Posted - Jul 09 2004 :  5:36:11 PM  Show Profile  Visit Sion's Homepage  Click to see Sion's MSN Messenger address  Reply with Quote
Eric, can you tell me what I've done wrong, if anything?

Visit my personal blog at www.AndersNissen.com!
Go to Top of Page

Eric Coleman
Gladiator

USA
811 Posts

Posted - Jul 09 2004 :  5:39:08 PM  Show Profile  Visit Eric Coleman's Homepage  Reply with Quote
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: Control_Array_Test.zip
2.5 KB
Go to Top of Page

Sion
Warrior

Denmark
138 Posts

Posted - Jul 09 2004 :  5:48:44 PM  Show Profile  Visit Sion's Homepage  Click to see Sion's MSN Messenger address  Reply with Quote
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! :)

Visit my personal blog at www.AndersNissen.com!
Go to Top of Page

Sion
Warrior

Denmark
138 Posts

Posted - Jul 27 2004 :  07:58:18 AM  Show Profile  Visit Sion's Homepage  Click to see Sion's MSN Messenger address  Reply with Quote
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!

  
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
  



Visit my personal blog at www.AndersNissen.com!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
VBGamer © Go To Top Of Page
This page was generated in 0.16 seconds. Snitz Forums 2000

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.