valuename = "IterativeApproximation"
elements = Array("resultError", "result", "iterations")


' demo 1: we just want to get the output to use somewhere else.
' this will echo it out
WScript.Echo UdtClass(valuename, elements)



Function UdtClass(valuename, elements)
        ' takes a value name and an array of value element names as arguments
        ' returns a class usable for creating structured values.
        ' I haven't looked at the complete history of questions about
        ' user-defined types in VBScript, but Jim Warrington pointed out this
        ' capability as far back as August 2001:
        ' http://groups.google.com/groups?threadm=OqKKe0LLBHA.1408@tkmsftngp05
        ' Note his point about possible performance penalties
        
        dim lines()
        ' blocks will be spaced by this number of lines
        dim spacers: spacers = 2
        dim LetLine: LetLine = vbTab & "public property let "
        dim GetLine: GetLine = vbTab & "public property get "
        dim EndLine: EndLine = vbTab & "end property"
        dim Indent: Indent = vbTab & vbTab
        dim Prefix: Prefix = "m_"
        dim eq: eq = " = "
        ' redim for class declaration and following blank lines
        redim lines(spacers + 1)
        ' create class statement line
        lines(0) = "Class " & valuename
        lines(1) = vbTab & "'This class autogenerated: " & Now()
        
        for i = 0 to Ubound(elements)
                if Len(elements(i)) > 0 Then
                        ' this loop dimensions the class-level variables
                        redim preserve lines(UBound(lines) + 1)
                        lines(UBound(lines)) = vbTab & "dim " & Prefix & elements(i)
                end if
        next

        for i = 0 to Ubound(elements)
                ' this loop adds the property lines
                if Len(elements(i)) > 0 Then
                        redim preserve lines(UBound(lines) + 6 + 2 * spacers)
                        lines(UBound(lines) - 5 - spacers) = _
                                LetLine & elements(i) & "(newValue)"
                        lines(UBound(lines) - 4 - spacers) = _
                                Indent & Prefix & elements(i) & eq & "newValue"
                        lines(UBound(lines) - 3 - spacers) = EndLine
                        lines(UBound(lines) - 2) = GetLine & elements(i)
                        lines(UBound(lines) - 1) = _
                                Indent & elements(i) & eq & Prefix & elements(i)
                        lines(UBound(lines)) = EndLine
                end if
        next        
        
        redim preserve lines(UBound(lines) + 1 + spacers)
        lines(UBound(lines)) = "End Class"
        UdtClass = Join(lines, vbLf)
End Function