Forum Discussion

dbattaglia's avatar
dbattaglia
Contributor
10 years ago

Iterate Properties of a VBScript Class Object

I was trying to implement a VBScript class in order to efficiently pass multiple pieces of information across scripts and hit a wall when I found that there is no built in method to iterate over the object's properties.

 

I found a functional answer here http://stackoverflow.com/questions/12569931/get-properties-of-vb-element shared by Emet M. and thought that there may be others who might benefit from this.  I made a couple minor changes in order to contain the pseudo-reflective stuff in the class.

 

Here is a small example, hope this helps!

 

class person
		' sample class containing pseudo-reflection properties and methods
		' wonky but genius. inspiration found here http://stackoverflow.com/questions/12569931/get-properties-of-vb-element
      ' create internal class variables
      private s_firstName                ' the person's first name
      private s_lastName                 ' the person's last name
      private s_phoneNumber              ' the person's phone number

      ' initialize event
      private sub class_initialize
              firstName = ""
              lastName = ""
              phoneNumber = ""
      end sub
      
      ' terminate event
'      private sub class_terminate
'              
'      end sub

      ' pseudo-reflection property.
      public property get propertyNames
             propertyNames = Array( "firstName", "lastName", "phoneNumber" )
      end property
      
      ' pseudo-reflection count property
      public property get propertyCount
             propertyCount = UBound(propertyNames) + 1
      end property
      
      ' pseudo-reflection valueGet method
      public function valueGet(propertyName)
             valueGet = Eval(propertyName)
      end function

      ' firstName property
      public property get firstName
             firstName = s_firstName
      end property
      
      public property let firstName(value)
             s_firstName = value
      end property

      ' lastName property
      public property get lastName
             lastName = s_lastName
      end property
      
      public property let lastName(value)
             s_lastName = value
      end property

      ' phoneNumber property
      public property get phoneNumber
             phoneNumber = s_phoneNumber
      end property
      
      public property let phoneNumber(value)
             s_phoneNumber = value
      end property

end class

	' you can iterate the properties of the object using the propertyNames array property
	' example for WScript
	dim genius
	set genius = new person
	
	genius.firstName = "Joe"
	genius.lastName = "Smith"
	genius.phoneNumber = "123-456-7890"
	
    for each propertyName in genius.propertyNames
        message = message & propertyName & ": " & genius.valueGet(propertyName) & vbnewline
    next
	
	WScript.Echo genius.propertyCount & " properties:"
	WScript.Echo message
		   
  • Hi dbattaglia,

     

    Thanks for sharing your example! Stack Overflow is a great knowledge base.

     

    By the way, TestComplete has built-in methods aqObject.GetProperties and aqObject.GetFields to iterate over an object's properties. They should work for VBScript classes too. GetProperties returns properties defined as "Property Get/Let/Set Something" and GetFields returns public fields defined as "Public Something".

     

    In your example, you can use:

    Dim props
    Set props = aqObject.GetProperties(genius) While props.HasNext Set prop = props.Next Log.Message prop.Name & ": " & aqConvert.VarToStr(prop.Value) ' & " (" & prop.Category & ")" Wend

    Hope this helps too! 

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi dbattaglia,

     

    Thanks for sharing your example! Stack Overflow is a great knowledge base.

     

    By the way, TestComplete has built-in methods aqObject.GetProperties and aqObject.GetFields to iterate over an object's properties. They should work for VBScript classes too. GetProperties returns properties defined as "Property Get/Let/Set Something" and GetFields returns public fields defined as "Public Something".

     

    In your example, you can use:

    Dim props
    Set props = aqObject.GetProperties(genius) While props.HasNext Set prop = props.Next Log.Message prop.Name & ": " & aqConvert.VarToStr(prop.Value) ' & " (" & prop.Category & ")" Wend

    Hope this helps too! 

    • dbattaglia's avatar
      dbattaglia
      Contributor

      Good grief... that is aweseme.

      I should have thought of that, but since I didn't thanks for sharing.

      GetProperties is simple and effective.

      Thanks Helen!