A little automation for VBScript OOP (1)

Categories: MS Excel DataSource code

Generate code for Property Let / Property Get / Property Set methods

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 - Skip. Used in only with delegation (see next post).

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 GenerateMethods1()
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
 PropertyType = UCase(Trim(DataSheet.Cells(RowIter, 3)))
 Select Case PropertyType
 Case "V", "S", "VARIANT"
 sOperator1a = "Let"
 sOperator1b = ""
 Case "P", "R", "POINTER", "REFERENCE"
 sOperator1a = "Set"
 sOperator1b = "Set"
 Case Else
 sOperator1a = "Let"
 sOperator1b = ""
 End Select
 InternalPrefix = Trim(DataSheet.Cells(RowIter, 4))
 ExternalPrefix = Trim(DataSheet.Cells(RowIter, 5))
 sIndent = DataSheet.Cells(RowIter, 6)
 CodeLine = sIndent & "Public Property Get " & PropertyName & vbCrLf
 CodeLine = CodeLine & sIndent & sIndent & sOperator1b & PropertyName & " = " & InternalPrefix & "_" & PropertyName & vbCrLf
 CodeLine = CodeLine & sIndent & "End Property" & vbCrLf
 CodeLine = CodeLine & vbCrLf
 CodeLine = CodeLine & sIndent & "Public Property " & sOperator1a & " " & PropertyName & "(ByVal " & ExternalPrefix & "_" & PropertyName & ")" & vbCrLf
 CodeLine = CodeLine & sIndent & sIndent & sOperator1b & InternalPrefix & "_" & 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 the author is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.