'Get properties of VB element
How can I see 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 use a Tool with a VBScript API. In that API I can read (user defined) attributes from that Tool. But while running the script I get an error telling me that it does not know the name of that user defined attribut. But I use it in the tool. Now I would like to know which attributes are availble in the array above to see if the user defined attributes are named specificly.
Solution 1:[1]
Not really possible. Only very basic type information is available in the VBScript runtime. Ideally you could create an adapter that translates your tool's objects into standard Dictionary objects and iterate the Keys. If that's not possible, the best you can do is check the type name for each object before invoking its members. Example:
<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>
Solution 2:[2]
Will you be able to get the list of properties from a script like this?
http://www.vbsedit.com/scripts/misc/wmi/scr_1332.asp
And, you can then use eval() or execute to get the values of these properties.
Solution 3:[3]
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
Solution 4:[4]
It is easy - use a 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
Solution 5:[5]
There's a way to get COM objects properties easily with the use of PowerShell. Either use the TAB key or pipe the object to Get-Member to list the properties. Not all COM objects allow their properties and methods to be listed with Get-Member, but the TAB can become handy in that specific situation. The Scripting.Dictionary is one case where Get-Member does not work.
$objTest = New-Object -ComObject Scripting.Dictionary
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | rizalp1 |
| Solution 3 | jainvikram444 |
| Solution 4 | Emet M. |
| Solution 5 | Fabrice Sanga |
