preload

A little automation for VBScript OOP (2)

Posted by Albert Gareev on Nov 27, 2008 | Categories: MS Excel DataSource code

Generate code for Property Let / Property Get / Property Set methods of Delegator Object

What to use

1. MS Excel worksheet as input / output interface

2. MS Excel VBA macro to generate source code

3. Input parameters

Property Name – External name (used as Property Let / Get name)

Parent Class – Name of the property to reference Parent class (inherit from).

Property Type – Variant (V) or Reference (R). Defines whether it’s Property Let or Set.

Internal Prefix – For creation of internal name of the property (E.g. use “p” for “p_X” property name).

External Prefix – For creation of Let / Set parameter name (E.g. use “in” for “in_X” parameter name).

Indent – type required number of Space characters there.

4. Output

After execution of the macro, Code cell will contain generated source code.

Source Code

Public Sub GenerateMethods2()

Set DataSheet = Workbooks(1).Worksheets(1).UsedRange

For RowIter = 2 To DataSheet.Rows.Count
    PropertyName = Trim(DataSheet.Cells(RowIter, 1))
    If PropertyName = "" Then PropertyName = "P" & RowIter
   
    ParentClass = Trim(DataSheet.Cells(RowIter, 2))
    If ParentClass <> "" Then ParentClass = ParentClass & "."
   
    PropertyType = UCase(Trim(DataSheet.Cells(RowIter, 3)))
    Select Case PropertyType
        Case "V", "S", "VARIANT"
            sOperator1a = "Let"
            sOperator1b = ""
            sOperator1c = "ByVal "
        Case "P", "R", "POINTER", "REFERENCE"
            sOperator1a = "Set"
            sOperator1b = "Set "
            sOperator1c = "ByRef "
        Case Else
            sOperator1a = "Let"
            sOperator1b = ""
            sOperator1c = "ByVal "
    End Select
   
    InternalPrefix = Trim(DataSheet.Cells(RowIter, 4)) ' not used
    ExternalPrefix = Trim(DataSheet.Cells(RowIter, 5))
    sIndent = DataSheet.Cells(RowIter, 6)
   
    CodeLine = sIndent & "Public Property Get " & PropertyName & vbCrLf
    CodeLine = CodeLine & sIndent & sIndent & sOperator1b & PropertyName & " = " & ParentClass & PropertyName & vbCrLf
    CodeLine = CodeLine & sIndent & "End Property" & vbCrLf
    CodeLine = CodeLine & vbCrLf
    CodeLine = CodeLine & sIndent & "Public Property " & sOperator1a & " " & PropertyName & "(" & sOperator1c & ExternalPrefix & "_" & PropertyName & ")" & vbCrLf
    CodeLine = CodeLine & sIndent & sIndent & sOperator1b & ParentClass & PropertyName & " = " & ExternalPrefix & "_" & PropertyName & vbCrLf
    CodeLine = CodeLine & sIndent & "End Property"
   
    DataSheet.Cells(RowIter, 7) = CodeLine

Next

End Sub


Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.