Results 1 to 16 of 16

Thread: [RESOLVED] Identify whether an object has a named property

  1. #1

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Resolved [RESOLVED] Identify whether an object has a named property

    I'm iterating through the controls collection, so I'm using On Error Resume Next to handle when the property I'm looking at (eg: TabIndex) doesn't exist for that control.

    Just wondering if there was a better or easier way to determine if a named property exists. Something like:

    If HasProperty(ctl, "TabIndex") Then
    ...

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Identify whether an object has a named property

    You can use the TypeLib Information library (TLBInf32.dll). I don't know about any documentation and have yet to search the net for more information. I just started with this library but using only the object browser for help was able to quickly write this code.

    Code:
    Private Sub Command1_Click()
        Dim ctl As Control
        For Each ctl In Controls
            If ControlHasProperty(ctl, "TabIndex") Then
                Debug.Print ctl.Name; " has the TabIndex Property"
            Else
                Debug.Print ctl.Name; " does not have a TabIndex property"
            End If
        Next
    End Sub
    
    Private Function ControlHasProperty(ctl As Control, PropName As String) As Boolean
        Dim info As TLI.InterfaceInfo
        Dim member As TLI.MemberInfo
        
        Set info = InterfaceInfoFromObject(ctl)
        For Each member In info.Members
            ControlHasProperty = StrComp(member.Name, PropName, vbTextCompare) = 0
            If ControlHasProperty Then Exit For
        Next
    End Function
    There are many more objects in this library that I need to explore and so am not sure if there is a better method - such as a Property Exists method
    Last edited by brucevde; Aug 16th, 2007 at 02:08 PM.

  3. #3

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Identify whether an object has a named property

    That's pretty cool. I'm a bit ignorant of Type Libraries in general; could you sum up an overview of the concept behind type libraries in a few sentences or less?

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Identify whether an object has a named property

    LOL. Describe the inner workings of COM in a few sentences! That is way beyond me...

  5. #5
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Identify whether an object has a named property

    I think Bruce's code is quite neat and clear, for some more info about TypeLib see This Post (from yesterday), specially the Links inside it. The First Link shows how to bring all members from DLL/OCX files (here is where the Object Browser takes VB Project's types from) to List/Treeview, the second Link shows how to list the Object's properties/Subs/functions.
    Maybe you can use the code in the 2nd link to ensure you found a property and not a method, or vice-versa.
    Last edited by jcis; Aug 16th, 2007 at 06:25 PM.

  6. #6

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Identify whether an object has a named property

    Quote Originally Posted by jcis
    I think Bruce's code is quite neat and clear,
    So do I; I'm just not clear on what a type library actually is.

  7. #7
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Identify whether an object has a named property

    The answer of your question is not Here? (From the first Link in the Thread I Linked before)
    TypeLib: "a set of COM objects designed to allow programmers to browse type libraries programmatically"

  8. #8

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Identify whether an object has a named property

    Quote Originally Posted by jcis
    The answer of your question is not Here? (From the first Link in the Thread I Linked before)
    TypeLib: "a set of COM objects designed to allow programmers to browse type libraries programmatically"
    Not so much. A type library lets you browse type libraries?

    And this bit cracked me up:
    If you're anything like me, you probably spend as much time using the Object Browser as you do actually writing code in Visual Basic®.
    Heh, I can count the number of times I've opened the Object Browser in my life on one hand, and I've never found it helpful.

  9. #9

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Identify whether an object has a named property

    Hmm, reading further gives a reasonable definition:
    Type libraries are files that explicitly describe some or all of the contents of components. This includes information about the methods, properties, constants, and other members exposed by the component. Development tools such as Visual Basic make use of the information contained in the type library to help you, as a developer, access and use the component. In addition, type libraries provide a convenient way to include a simple level of descriptive documentation for component members.

    If you develop COM components using Visual Basic, a type library is automatically created for you when you compile the component. In addition, you can create your own type libraries for your own components. There are a few different ways to do this, but for those who program in Visual Basic a favorite tool is MIDL. This tool allows you to compile IDL files into binary type libraries. In this way, you could, for example, build a type library to encapsulate and expose a set of API calls that you often use. Then, instead of using declare statements in your Visual Basic source code, all you would have to do is set a reference to your custom API type library.
    This doesn't appeal to me in any way, though. In fairness, I'm not a real programmer, but rather a business applications developer. All my work is geared toward small database programs with relatively few users, so I rarely have need of any advanced stuff.

    As for the example in that quote, wrapping up API calls into a type library seems counter-productive to me. From my perspective, the main advantage of API calls is that they reduce the number of dependencies in a given project. I don't like dependencies; in fact, I rather dislike them. I don't even like using user controls that I wrote, even if I include the user control source code directly in the project.

    Classes I can get behind, but anything requiring dependencies leaves me cold.

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Identify whether an object has a named property

    Quote Originally Posted by Ellis Dee
    Not so much. A type library lets you browse type libraries?
    This Type Library lets you browse trought any exposed Object members. These Objects can be in the VB Project/OCXs/Dlls/Registry..)

    Example on how VB uses it: You go to Components and check "Microsoft Flexgrid .." then VB uses this library to Open de Flexgrid's OCX file and starts searching for members (exposed Constants/Types/Enums/properties/methods), you can see this info in the Object Browser.

    The same way you can use the TypeLib yourself to browse into any COM Object.

  11. #11

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Identify whether an object has a named property

    Quote Originally Posted by jcis
    Example on how VB uses it: You go to Components and check "Microsoft Flexgrid .." then VB uses this library to Open de Flexgrid's OCX file and starts searching for members (exposed Constants/Types/Enums/properties/methods), you can see this info in the Object Browser.
    So a type library in effect gives you intellisense information without requiring an IDE?

  12. #12
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Identify whether an object has a named property

    Quote Originally Posted by Ellis Dee
    Classes I can get behind, but anything requiring dependencies leaves me cold.
    You don't like to create package Installers don't you?

  13. #13
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Identify whether an object has a named property

    Quote Originally Posted by Ellis Dee
    So a type library in effect gives you intellisense information without requiring an IDE?
    Exactly that is And that's how Static used it in the StaticToolz thread, in the first Link of my post.

  14. #14

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Identify whether an object has a named property

    Quote Originally Posted by jcis
    You don't like to create package Installers don't you?
    I can't stand installation period.

  15. #15

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Identify whether an object has a named property

    Sorry guys, I got the "you must spread some reputation around" notice on both of you. So here's a public thanks for your help.

  16. #16
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: [RESOLVED] Identify whether an object has a named property

    Did someone say "Installation"?....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width