Get VB Item Properties

How to find out what properties an element has in a VB script? Example:

Dim list : Set list = CreateObject( "Scripting.Dictionary" )
' ... Fill List ...
WriteListElements list
...

Sub WriteListElements ( list )
    Dim e, le
    For Each e In list
        Set le = list(e)                  ' what properties does le have?
        le.name_of_user_defined_attribut  ' I want to access a property but dont know the exact name
    Next
End Sub

I am using a tool with the VBScript API. In this API, I can read (user-defined) attributes from this Tool. But when I run the script, I get an error message indicating that he does not know the name of the user-specified attribute. But I use it in a tool. Now, I would like to know what attributes are available in the above array to find out if user-defined attributes are specified.

+5
source share
4 answers

. VBScript . , . , , , . :

<html>
<body>

<script type="text/vbscript">

    Class Human
        Private m_name

        Public Property Get Name
            Name = m_name
        End Property

        Public Property Let Name(newName)
            m_name = newName
        End Property
    End Class

    Dim joe 
    Set joe = new Human
    joe.Name = "Joe Coder"

    Dim list
    Set list = CreateObject( "Scripting.Dictionary" )
    list.Add "a", 5
    list.Add "b", joe
    list.Add "c", "apples"

    WriteListElements list

    Sub WriteListElements ( list )
        Dim e
        For Each e In list
            If (TypeName(list.Item(e)) = "Human") Then
                document.write("We have found a Human: " &_
                    "<b>" & list.Item(e).Name & "</b>")
            End If
        Next
    End Sub

</script>

</body>
</html>
+2
Dim list : Set list = CreateObject( "Scripting.Dictionary" )
' ... Fill List ...
WriteListElements list
...

Sub WriteListElements ( list )
    Dim e, le     
    For Each e In list
        Set le = e.Items                
        Response.Write le(name_of_user_defined_attribut)
    Next
End Sub
+1

You can get the list of properties from the script as follows:

http://www.vbsedit.com/scripts/misc/wmi/scr_1332.asp

And you can use eval () or execute to get the values ​​of these properties.

0
source

Easy - use pseudo-reflection:

   class Developer

      Public reflection
     '=============================
     'Private properties
      private mId

      private  mFirstName
      private  mLastName

      private sub Class_Initialize()
        reflection = Array("Id","FirstName","LastName")
      end sub

      private sub Class_Terminate()
      end sub

     '=============================
     'public properties

       public property get Id()
          Id = mId
       end property

       public property let Id(val)
          mId = val
       end property


       public property get FirstName()
          FirstName = mFirstName
       end property  

       public property let FirstName(val)
          mFirstName = val
       end property  

       public property get LastName()
          LastName = mLastName
       end property  

       public property let LastName(val)
          mLastName = val
       end property  

    end class


    For each property in obj.reflection 
     document.write(property)
     document.write( Eval ("obj." & property) )
    Next 
0
source

All Articles